Support

Account

Home Forums Add-ons Repeater Field Repeater sub field display by group based on value

Solving

Repeater sub field display by group based on value

  • Hi there,

    I’m trying and struggling to find the right way to go about this…

    I’ve got a list of names that belong to a group… both are setup a sub fields of a repeater group ‘Person Details’.

    Persons Details has two sub-fields: 1) Group Name, 2) Person Name

    e.g.

    Group name: A, Person Name: Joe Bloggs
    Group name: A, Person Name: Jane Doe
    Group name: B, Person Name: Harry Potter

    I want to be able to output a list of names, but broken up into the relevant groups as various sections.

    Any pointers gratefully received.

  • I was trying to do the same thing a while ago. Couldn’t figure out how to do it natively in ACF so I just made a normal PHP array, sorted it, then looped through it.

    Full credit to the following (my solution is basically these stuck together):

    http://stackoverflow.com/questions/5498718/how-to-sort-associative-array-using-sub-field-of-contained-associative-arrays-in
    https://www.advancedcustomfields.com/resources/repeater/

    
    <?php
    
    // check if the repeater field has rows of data
    if( have_rows('person_repeater') ):
    
        $personArray = get_field('person_repeater');
    
        function callback( $a, $b ) {
    
            if ( $a['person_group'] > $b['person_group'] ) {
    
                return 1;
    
            } else if ( $a['person_group'] < $b['person_group'] ) {
    
                return -1;
    
            }
    
            return 0;
    
        }
    
        uasort( $personArray, 'callback' );
    
        foreach ($personArray as $person) {
    
            ?>
    
            Group name: <?php echo $person['person_group']; ?>, Person Name: <?php echo $person['person_name']; ?><br>
    
        <?php
    
        }
    
    else :
    
        // no rows found
    
    endif;
    
    ?>
    
  • Nearly there – thank you!

    I basically just want to be able to output a grouped list, for each group – if that makes sense? The above renders: Group Name and Person Name for every entry.

    End result being:

    Group 1

    • Name 1
    • Name 2
    • Name 3

    Group 2

    • Name 4
    • Name 5
    • Name 6

    Group 3

    • Name 7
    • Name 8
    • Name 9
  • If the array is sorted in the right order something like this will show them… I’m guessing at a bit of this because you didn’t actually give the field names and you’ll need to adjust the loop to output what you want. This is just an example of how to loop through an array that has been grouped together by one of it’s elements.

    
    $last_group = ''; // initialize the group name
    foreach ($personArray as $person) {
      $this_group = $person['group_name'];
      if ($this_group != $last_group) {
        echo '<h2>',$person['group_name'],'</h2>';
      }
      $last_group = $this_group;
      echo '<p>'.$person['person_name'].'</p>';
    }
    
  • Hi John, many thanks for the above… still struggling a tad…

    In case it helps, my repeater fields are structured as follows:

    Field Group = company_information

    Sub field 1 = company_group (value as integer)
    Sub field 2 = company_name (text)
    Sub field 3 = company_website (optional) – to be wrapped around company_name if present.

    So ideally, I’d end up with the following

    <h2>group_name 1</h2>

    • company_name 1
    • company_name 2 – with a link around it if company_website has a value

    <h2>group_name 2</h2>

    • company_name 3
    • company_name 4 – with a link around it if company_website has a value

    Thanks so much thus far – feels almost within reach!

  • including @friendofdog‘s help

    
    function shortByGroup($a, $b) {
      if ($a['company_group'] > $b['company_group']) {
        return 1;
      } elseif ($a['company_group'] < $b['company_group']) {
        return -1;
      }
      return 0;
    }
    
    $personArray = get_field('company_information');
    if (!empty($personArray)) {
      usort($personArray, 'sortByGroup');
      $last_group = ''; // initialize the group name
      $count = 0;
      foreach ($personArray as $person) {
        $this_group = $person['company_group'];
        if ($this_group != $last_group) {
          if ($count > 0) {
            echo '</ul>';
          }
          echo '<h2>',$person['company_group'],'</h2><ul>';
        }
        $company = $person['company_name'];
        if ($person['company_website']) {
          $company = '<a href="'.$person['company_website'].'">'.
                         $company_name.'</a>';
        }
        echo '<li>',$company,'</li>';
      } // end foreach
    } // end if personArray
    echo '</ul>';
    
  • Hi John, thanks a lot for this piece of code.
    It won’t work until we correct the sort function namespace 😉 (sortByGroup and not shortByGroup).
    Something else : from this code, the company group (h2) is displayed for every row because it’s in the foreach loop.
    Do you think there’s a way to get the value of company group and display it only one time per group ?

  • With the help of a collegue, I rewrote the code for my purpose (and it works) :

    function sortByGroup($a, $b) {
    		if ($a['partner-category'] > $b['partner-category']) {
        		return -1;
      		} elseif ($a['partner-category'] < $b['partner-category']) {
        		return 1;
      		}
      		return 0;
    	}
    
    	$partnerArray = get_field('partner');
    	if (!empty($partnerArray)) {
      		usort($partnerArray, 'sortByGroup');
      		$last_group = ''; // initialize the group name
      		foreach ($partnerArray as $partner) {
        		$this_group = $partner['partner-category'];
        		if ($this_group != $last_group) {
        			$last_group = $this_group;
          			echo '<h3>',$partner['partner-category'],'</h3>';
        		}
        		if ($partner['partner-website']) {
          			$partner_bloc = '<a href="'.$partner['partner-website'].'">'.$partner['partner-name'].'</a>';
        		}
        		echo $partner_bloc;
      		} // end foreach
    	} // end if personArray

    The line $last_group = $this_group; was missing in order to display the H3 only one time by group. And the sorting function has been inverted in order to reflect the backend order (from top to bottom).

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

You must be logged in to reply to this topic.