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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Compare to previous values in control loop


Go to End


3 Posts   2388 Views

Avatar
esakrielaart

Community Member, 59 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.