I am using the following to display image icons to represent the operating systems of a device.

<?php if($_product->getoperating_system()): ?><li><?php $os=$_product->getAttributeText('operating_system');
echo '<a href="/catalogsearch/advanced/result/?operating_system%5B%5D='.str_replace(' ', '_',$osid).'"><img class="specicon" src="/media/avenueimages/icons/'.str_replace(' ', '_',$os).'.png" alt="'.$os.'" title="'.$os.'"></a><p>'.$os.'</p>' ;?></li><?php endif; ?>

Results are as shown here: enter image description here

I would like to do the same with a multiple select attribute.

This code displays the attribute value ids:

<?php echo $_product->getData('printer_functions'); ?>

How do I modify the first code to display an icon for each selected value?


For multiple select attributes you can get the selected values like this:

$ids = $_product->getPrinterFunctions();

This returns an array or a string with the values concatenated by comma depending on the value for the flat catalog products flag.
So do this next.

if (!is_array($ids)) {
    $ids = explode(',', $ids);

Now you can loop through the $ids and do something with each id.
You can get the labels of the options like this

$texts = $_product->getAttributeText('printer_functions');

This will get you a string with the labels separated by comma.
So you can make that an array like this:

$texts = explode(',', $texts);

If everything is OK then $ids and $texts arrays should have the same count.

And small tip:
Don't use echo '<a href="/catalogsearch/advanced/result/?operating_system%5B%5D='.str_replace(' ', '_',$osid).'"> It will not work if your magento instance is located in one folder.
Use this instead:

 $url = Mage::getUrl('catalogsearch/advanced/result', array('_query'=> 'operating_system[]='.str_replace(' ', '_',$osid)));

 echo '<a href="'.$url.'"'>...'

Here is the code that has worked for me. I have modified code i found online:

<?php if($_product->getprinter_functions()): ?>
<?php $sportTypes = explode(",",$_product->getResource()

foreach($sportTypes as $key => $value){
        echo '<li><img class="specicon" src="/media/avenueimages/icons/'.str_replace(' ', '_',$value).'.png" alt="'.$value.'" title="'.$value.'"></a><p>'.$value.'</p></li>';

<?php endif; ?>

Here are the results:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.