Group records in same family
3 posts by 3 authors in: Forums > CMS Builder
Last Post: September 9, 2015 (RSS)
By celuch - September 4, 2015 - edited: September 4, 2015
I'm trying to display a list of catalog items, all with individual skus, but many with different sizes. All like items are labled with a family name, which is the same as the sku without the size code. There is a photo of only one size in each family. Is there a way to group all "families" and only show one record (in this case, the one with a photo?). Or at least limit, possibly through a select distinct or where statement so it limits the number of records to display, so that it will show all products with an image?
Hi Celuch,
Don't know if this recipe from my CMSB Cookbook http://www.thecmsbcookbook.com will help, but it does something similar with available colors for a catalog item.
Best,
Jerry Kornbluth
DISPLAYING 'AVAILABLE COLORS" IMAGES ON A DETAIL PAGE IF COLOR IS CHECKED IN A LIST FIELD
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>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Hi Celuch,
I'd recommend looking into the array_groupBy function that's built into CMS builder:
// array_groupBy:
// eg. $recordsByNum = array_groupBy($records, 'num');
// eg. $recordsByCategory = array_groupBy($records, 'category', true);
$result = array_groupBy($recordList, $indexField, $resultsAsArray = false)
An example of how to use this would be:
$blogsSortedByCategory = array_groupBy($blogs, 'category', true);
In this example all of my blogs would be sorted by category in a nested array, so I could loop through them like this:
<?php foreach($blogsSortedByCategory as $category => $blogs): ?>
<h1><?php echo $category</h1>
<?php foreach ($blogs as $key => $blog): ?>
<h3><?php echo $blog['title']; ?></h3>
<?php echo $blog['content']; ?>
<?php endforeach; ?>
<?php endforeach; ?>
Thanks,
Greg
PHP Programmer - interactivetools.com