Reputation: 97
I'm trying to create several arrays of data from a repeater. There are 3 columns to each row;
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;
Upvotes: 3
Views: 8271
Reputation: 1246
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/
Upvotes: 2