views:

759

answers:

1

I'm trying to retrieve products that carry same attribute. Specifically multiple select type. It seems the basic methods don't work. Selecting only the "name" attribute, I get all my products listed. When I try to filter "shop_by_color", it filters down, but not entirely. Not sure why it removes some and leaves others, even though they are the wrong ones. Any tips appreciated.

<?php 

$model = Mage::getModel('catalog/product');

$collection = $model->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToFilter('shop_by_color'); // multiple select attribute
$collection->addFieldToFilter(array(array('attribute'=>'shop_by_color','finset'=>array('Yellow, White'),
)));

$collection->load();

?>

<ul>
<?php foreach($collection as $product) : ?>
<li><a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName() ?></a></li>            
<?php endforeach; ?> 
</ul>
A: 

Hi I am not sure about your syntax I have never seen this sort of thing before.

<ul>
<?php foreach($collection as $product) : ?>
<li><a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName()
 ?></a></li>            
<?php endforeach; ?> 
</ul>

Shouldn't it be...

<ul>
<?php foreach($collection as $product) { ?>
<li><a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName()
 ?></a></li>            
<?php } ?> 
</ul>

DC

DeveloperChris
Apparently foreach() : endforeach; is legitimate syntax. who knew! I still wouldn't use it
DeveloperChris
Thanks for the try DeveloperChris. This syntax is pretty much what I see used for Magento. It works fine, Do you think your method will speed up loading if implemented site-wide? I mostly need help with the filtering option. If you're familiar with Magento, I appreciate any help.
monocat
No it won't make any difference to the speed, the compilation time might improve by a fraction but that's all. Using a php accelerator like eaccelerator is a good option. Sorry I don't know magento at all.To locate the source of your original problem I would enable query logging in mysql(assumed) then look at the actual query sent to the DB. if that returns the correct results then you need to step through the code to find the error.
DeveloperChris