Support

Account

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

  • 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>';