Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Compare to previous values in control loop


Reply


3 Posts   823 Views

Avatar
esakrielaart

Community Member, 56 Posts

1 October 2011 at 4:46am

Hello,

I'm trying to group some items based on their groupID. So far, I managed to do with:

static $default_sort = 'groupID DESC';


Now I'm trying to read data into my template using the control-loop which works fine, showing all data sorted as defined. But now what I want, is the groupID to be displayed only if it differs from the previous value. Is there some way of saying:

// Pseudo code
if thisGroupID equals previousGroupID then
.
.
.


Thanks in advance,
Maurice

Avatar
ryanwachtl

Community Member, 46 Posts

4 October 2011 at 7:32am

Within the template control loop you have access to the methods of the current item being iterated over in the loop and the items parent ($Top.) methods. Normally I would create a Group DataObject and Item DataObject. Then assign the appropriate relationships ($has_many, $has_one, etc.). Once that is done you can loop through each Group and within the Group loop through it's Items. Beyond that I believe you'll have to write your own methods in your PHP Class to do the logic.

Avatar
ajshort

Community Member, 244 Posts

4 October 2011 at 9:28am

You might want to see http://doc.silverstripe.org/sapphire/en/howto/grouping-dataobjectsets/ for how to group a DataObjectSet by one of its fields.