Support

Account

Home Forums Front-end Issues Grouping repeater rows together on the front end (rows have the same id)

Solving

Grouping repeater rows together on the front end (rows have the same id)

  • Hi,

    Currently, I’ve got an href that’s pulling the row name and assigned ID into it, and then I have various other fields within the row, such as colours from linked relationship fields, that are being pulled in to the modal popup (frontend html screenshot attached). It’s essentially a colour picker with the href acting as the link for the relevant popup, both of which use a data-id assigned via a group_id number field within the repeater row on the backend (one field, same number gets applied to each element). The functionality all works fine, but what I want to do is query whether or not there are multiple rows with the same group_id, and if there are then I’d like the contents of those rows to be contained within the same modal popup instead of displaying two links / popups for rows with the same ID.

    If it’s possible then I’d appreciate someone pointing me in the right direction. If not, then I’d also appreciate someone letting me know so I’m not searching around for an answer that doesn’t exist.

    front end html

    Thanks!
    Matt

  • There really isn’t any way to do this because there really isn’t a way to know ahead of time what is in other rows.

    What you’d need to do is to loop through all of the rows and collect the information into an array that is sorted in whatever way you want. Then you can loop over the groups in the array.

    For example:

    
    $groups = array();
    if (have_rows('repeater') {
      while (have_rows('repeater')) {
        the_row();
        $groups[get_sub_field('grouping_field')][] = array(
          'field_1' => get_sub_field('sub_field_1'),
          'field_2' => get_sub_field('sub_field_2'),
          'field_3' => get_sub_field('sub_field_3'),
          // etc...
        );
      }
    }
    
    if ($groups) {
      foreach ($groups as $grouping_value => $group) {
    
      }
    }
    
  • Thanks John! That makes sense, I’ll give this a go and see how it goes. Thanks for the assistance!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Grouping repeater rows together on the front end (rows have the same id)’ is closed to new replies.