Support

Account

Home Forums General Issues Loop through all rows of ACF group without specifying row name

Solved

Loop through all rows of ACF group without specifying row name

  • In my advanced custom fields I have a group within a group.

    The fields within the inner group are to select parts of meals. Apart from the first row within that group, the format will be exactly the same

    enter image description heremy groups

    I’ve written code that outputs the food name and the value associated with each one (multiplied by another variable).

    As well as outputting those, the 5th and 6th row have an if statement. They only show if another variable $feeds_per_day is as high as that meal number.

    Here is the code…

    if( have_rows('zero') ):
    
    ?><div class="grid-50" style="float: left;"><table><tr><th colspan="2">Zero</th></tr><?php
    
    while( have_rows('zero') ) : the_row(); ?>
    
            <tr><td>Upon Waking</td><td><?php the_sub_field('upon_waking')?></td></tr>
    
            <?php 
        if ( have_rows('meal_1') ){ 
            while( have_rows('meal_1') ) {
            the_row();
    
            $protein_object = (get_sub_field_object('protein_select'));                                 
            $protein_value = get_sub_field('protein_select');                                   
            $protein_label = ($protein_object['choices'][$protein_value]);
    
            $carb_object = (get_sub_field_object('carb_select'));                                   
            $carb_value = get_sub_field('carb_select');                                 
            $carb_label = ($carb_object['choices'][$carb_value]);
    
            $fat_object = (get_sub_field_object('fat_select'));                                 
            $fat_value = get_sub_field('fat_select');                                   
            $fat_label = ($fat_object['choices'][$fat_value]);
    
            $protein_amount = round($zero_protein_feed / $protein_value * 10) *10;
            $fat_amount = round($zero_fat_feed / $fat_value * 10) *10 ;
    
            ?><tr><td>Meal 1</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php }  }
    
            if ( have_rows('meal_2') ){ 
            while( have_rows('meal_2') ) {
            the_row();
    
            $protein_object = (get_sub_field_object('protein_select'));                                 
            $protein_value = get_sub_field('protein_select');                                   
            $protein_label = ($protein_object['choices'][$protein_value]);
    
            $carb_object = (get_sub_field_object('carb_select'));                                   
            $carb_value = get_sub_field('carb_select');                                 
            $carb_label = ($carb_object['choices'][$carb_value]);
    
            $fat_object = (get_sub_field_object('fat_select'));                                 
            $fat_value = get_sub_field('fat_select');                                   
            $fat_label = ($fat_object['choices'][$fat_value]);
    
            $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
            $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;
    
            ?><tr><td>Meal 2</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }
    
            if ( have_rows('meal_3') ){ 
            while( have_rows('meal_3') ) {
            the_row();
    
            $protein_object = (get_sub_field_object('protein_select'));                                 
            $protein_value = get_sub_field('protein_select');                                   
            $protein_label = ($protein_object['choices'][$protein_value]);
    
            $carb_object = (get_sub_field_object('carb_select'));                                   
            $carb_value = get_sub_field('carb_select');                                 
            $carb_label = ($carb_object['choices'][$carb_value]);
    
            $fat_object = (get_sub_field_object('fat_select'));                                 
            $fat_value = get_sub_field('fat_select');                                   
            $fat_label = ($fat_object['choices'][$fat_value]);
    
            $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
            $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;
    
            ?><tr><td>Meal 3</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }
    
            if ( have_rows('meal_4') ){ 
            while( have_rows('meal_4') ) {
            the_row();
    
            $protein_object = (get_sub_field_object('protein_select'));                                 
            $protein_value = get_sub_field('protein_select');                                   
            $protein_label = ($protein_object['choices'][$protein_value]);
    
            $carb_object = (get_sub_field_object('carb_select'));                                   
            $carb_value = get_sub_field('carb_select');                                 
            $carb_label = ($carb_object['choices'][$carb_value]);
    
            $fat_object = (get_sub_field_object('fat_select'));                                 
            $fat_value = get_sub_field('fat_select');                                   
            $fat_label = ($fat_object['choices'][$fat_value]);
    
            $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
            $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;
    
            ?><tr><td>Meal 4</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }
    
            if ( have_rows('meal_5') ){ 
            while( have_rows('meal_5') ) {
            the_row();
    
            $protein_object = (get_sub_field_object('protein_select'));                                 
            $protein_value = get_sub_field('protein_select');                                   
            $protein_label = ($protein_object['choices'][$protein_value]);
    
            $carb_object = (get_sub_field_object('carb_select'));                                   
            $carb_value = get_sub_field('carb_select');                                 
            $carb_label = ($carb_object['choices'][$carb_value]);
    
            $fat_object = (get_sub_field_object('fat_select'));                                 
            $fat_value = get_sub_field('fat_select');                                   
            $fat_label = ($fat_object['choices'][$fat_value]);
    
            $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
            $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;
    
                if ($feeds_per_day > 4){
    
            ?><tr><td>Meal 5</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }
    
                }
    
            if ( have_rows('meal_6') ){ 
            while( have_rows('meal_6') ) {
            the_row();
    
            $protein_object = (get_sub_field_object('protein_select'));                                 
            $protein_value = get_sub_field('protein_select');                                   
            $protein_label = ($protein_object['choices'][$protein_value]);
    
            $carb_object = (get_sub_field_object('carb_select'));                                   
            $carb_value = get_sub_field('carb_select');                                 
            $carb_label = ($carb_object['choices'][$carb_value]);
    
            $fat_object = (get_sub_field_object('fat_select'));                                 
            $fat_value = get_sub_field('fat_select');                                   
            $fat_label = ($fat_object['choices'][$fat_value]);
    
            $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
            $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;
    
                if ($feeds_per_day > 5){
    
            ?><tr><td>Meal 6</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }
    
                }
    
    endwhile;
    
            ?></table></div><?php
            endif; 

    … apart from the first row, I’m repeating a lot of the same code. Is there a way to cycle through all of the fields and apply the one format if the field is ‘upon_waking’ and another format if not?

  • 
    for ($i=1; $i<7; $i++) {
      if (have_rows($meal.$i)) {
        while (have_rows('meal_'.$i) {
          the_row();
          
          // get sub fields and do calulations
          
          ?><tr><td>Meal <?php echo $i; ?></td><td><?php // your code continues...
        } // end while
      } // end if meal
    } // end for
    
  • Thanks John,

    Think there were a couple of tiny typos but that works great. And I added my feeds_per_day variable in the for loop to show the right amount of meals. Perfect!

    for ($i=1; $i<$feeds_per_day + 1; $i++) {
    if (have_rows('meal_'.$i)) {
        while (have_rows('meal_'.$i)) {
          the_row();
    		
          // get sub fields and do calulations
          
          ?><tr><td>Meal <?php echo $i; ?></td><td><?php echo more stuff ?></td></tr><?php
        } // end while
      } // end if meal
    } // end for
  • This did solve my initial problem, but how would it work if the inner rows weren’t named sequentially?

    I have four groups in a parent group called carb_cycle_meals, but the inner groups are named ‘zero’, ‘low’, ‘medium’ and ‘high’. I want to perform exactly the same code for each of them.

    Is that possible?

  • I would use an array with the repeater name, for example.

    
    $repeaters = array('zero', 'low', 'medium', 'high');
    foreach ($repeaters as $repeater) {
      if (have_rows($repeater)) {
        while (have_rows($repeater)) {
          the_row();
          // etc...
        }
      }
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Loop through all rows of ACF group without specifying row name’ is closed to new replies.