Support

Account

Home Forums Add-ons Repeater Field Only last repeater entry is accessible in my loop? Reply To: Only last repeater entry is accessible in my loop?

  • Hi guys,

    Thanks for your help firstly! I’m really impressed with the quality of this plugin, I know I’m trying to do something tricky here with only a working knowledge of PHP. Everything else I’ve used ACF for (lots of applications!) works like a charm straight out of the box.

    Now.

    I tried removing the postdata functions, but that doesn’t seem to have changed anything.

    Taking the $idlist declaration out of the loop does seem to have helped- I set up two test repeater rows and posts chosen in both are being affected.

    However – only the values from the last repeater row are being applied. I.e. I have two sales, one takes $50 off a group of posts, the second takes $150. Now all the posts selected are showing $150 reduction.

    This is the current code I’ve put in a test page template to get the print:

    if(get_field('sales', 'option')):
     
    	while(has_sub_field('sales', 'option')):
     
    		$saletype = get_sub_field('sale_type');
    		$discount = get_sub_field('discount_amount');
    		$saleitems = get_sub_field('devices_on_sale');
    
    		if( $saleitems ):
    		    foreach( $saleitems as $post):
    				$thisid = $post->ID;
    				$idlist[] = $thisid;
    		    endforeach;
    		endif;
    		
    		echo($discount);
    		print_r($idlist);
     
    	endwhile;
     
    endif;

    Which yields:
    100Array ( [0] => 3391 [1] => 3410 [2] => 3412 ) 150Array ( [0] => 3391 [1] => 3410 [2] => 3412 [3] => 1292 [4] => 751 )

    So by my understanding, this is adding both groups of selected posts to the same array.

    Perhaps during the foreach loop, the $post->$ID can be stored differently… I have a vague idea – what I need is not a single named array, but some kind of anonymous array that can be referenced differently somehow, when I need it?

    Alternatively, could I create a “name” text subfield for each repeater row to provide each row with a unique identifier, then reference the related array from that somehow?

    Argh! Any further help much, much appreciated! Cheers for having such great support!