Listing field values only once when they appear in a grouped list

3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 3, 2019   (RSS)

By daniel - May 3, 2019

Hi Jerry,

It looks like you already have part of a solution with you're handling the Country. This could be expanded to also handle state/province (duplicate the variables and add another if() condition), though it gets somewhat messy when trying to handle two sets of values like this.

An alternate idea that I think could work is adding a 'groupBy' to your getRecords, like this:

list($accountsRecords, $accountsMetaData) = getRecords(array(
  'tableName' => 'accounts',
  'orderBy' => 'practice_country ASC , practice_state ASC ',
  'groupBy' => 'practice_country, practice_state',
));

This will tell effectively tell the query to "group" records that have the same values for both 'practice_country' and 'practice_state'. Grouping has a few different uses, but in this case, the important thing is that it should only return one record per group, meaning only one record per country/state pair.

Try this out and let me know how it goes!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - May 3, 2019

Wow, was that easy.

Never knew about groupBy.

Pretty handy...

Just added it to the existing page and it worked like I really knew what I was doing.

Thanks Daniel, have a great weekend...

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php