DISPLAYING 'AVAILABLE COLORS" IMAGES ON A DETAIL PAGE IF COLOR IS CHECKED IN A LIST FIELD - Jul 25th, 2013


In a simple store scenario was trying to display images on a detail page for all colors that are checked as being
available for a particular item.

This was my approach:

First I created an array ($colors1) which contains a comma separated list of all of the colors that are available for
the item.

Next I uploaded images representing all of the available colors in a multi-image upload field and entered the color of
the image in the info1 field for each image.

Then in a foreach loop, I used the strpos function to determine if the info1 value exists in the $colors1 variable and
if it does, display that thumbnail image.

Problem was, the image corresponding to the first color in the $colors1 variable was always skipped, and I couldn’t
figure out why.

Greg Thomas from Interactive Tools postulated that it might be because the first item in the array doesn't have a
leading comma and space, and so as far as the strpos function is concerned it’s not an exact match.

He suggested using the in_array function instead to detect if the color has been selected (was in the array) or not.
Yes, the order of the variables in the 2 functions are opposite from each other, but that’s just the way they need to
be to work.

Here’s the recipe that came out of the discussion:

<table align= “center”>
<tr>
<td colspan="2">
<?php $colors1 = join(', ', $store_inventoryRecord['colors:labels']); ?>
<span class="navigation_font">Available Colors: <?php echo $colors1 ?></span><br />
</td>
</tr>
<tr>
<?php foreach ($store_inventoryRecord['image'] as $upload): ?>
<?php $info1 = $upload['info1'] ?>
<?php // if(strpos($colors1, $info1)): // this doesn’t work?>
<?php if(in_array($info1,$store_inventoryRecord['colors:labels'])): // This works ?>
<td align="center">
<span class="sub_heading_font"><?php echo $upload['info1'] ?></span><br />
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo
$upload['thumbHeight'] ?>" alt="" />
</td>
<?php endif ?>
<?PHP $maxCols=2; if (@++$count % $maxCols == 0): ?></tr><tr><?PHP endif; ?>
<?php endforeach ?>
</tr>
</table>



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service