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?

celuch

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

Greg Thomas







PHP Programmer - interactivetools.com