3

I'm trying to create several arrays of data from a repeater. There are 3 columns to each row;

  1. bus_type
  2. band_food
  3. band_no_food

The first col is TEXT and the other 2 are select lists.

What I'd like to do is loop through the results and depending on the condition, store the "bus_type" value in an array whihc I can later use as a conditional statement. I thought something like this would work, but not having any luck;

$foo_a = array();
$foo_b = array();

if( have_rows('bus_type') ): while ( have_rows('bus_type') ) : the_row();

    if (get_sub_field('band_food') == 'a') {
        $foo_a[] = get_sub_field('bus_type');
    } else if (get_sub_field('band_food') == 'b') {
        $foo_b[] = get_sub_field('bus_type');
    }

endwhile; endif;

Any ideas hows I would achieve the desired result?

SOLVED

OK, it looks like storing this as a VAR works. The final code would look like this;

if( have_rows('bus_type') ): while ( have_rows('bus_type') ) : the_row();

    $food_var = get_sub_field('band_food');

    if ($food_var == 'a') {
        $foo_a[] = get_sub_field('bus_type');
    } else if ($food_var == 'b') {
        $foo_b[] = get_sub_field('bus_type');
    }

endwhile; endif;
user1235285
  • 87
  • 2
  • 17

1 Answers1

2

This might work. It loops through a given repeater field, checks if the value matches hamburger and registers the bus type in the array whihc if theres a match.

<?php
$whihc = array();

// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):

    // loop through the rows of data.
        while ( have_rows('repeater_field_name') ) : the_row();

            // check for some condition and register bus type in array.
            if( the_sub_field('band_food') == 'hamburger' ):
               $whihc[] = get_sub_field('bus_type');
            endif;
        
        endwhile;

endif;

?>

Most of the code is taken from here: http://www.advancedcustomfields.com/resources/repeater/

Faye
  • 134
  • 8
danjah
  • 1,226
  • 1
  • 12
  • 27
  • 1
    Sadly not, this is essentially what I have in my provided example apart from this code will output the data, hence using get_sub_field above. It seems the condition does not work, as removing it will add the data to the array. – user1235285 Dec 11 '15 at 00:31
  • 1
    get_sub_field (return value) instead of the_sub_field (echo value) – Nadav May 07 '20 at 09:17