Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Compare to previous values in control loop

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

Page: 1
Go to End
Author Topic: 767 Views
  • esakrielaart
    Avatar
    Community Member
    54 Posts

    Compare to previous values in control loop Link to this post

    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

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: Compare to previous values in control loop Link to this post

    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.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Compare to previous values in control loop Link to this post

    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.

    767 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.