Support

Account

Home Forums General Issues Retrieve multiple checkbox values outside foreach loop

Helping

Retrieve multiple checkbox values outside foreach loop

  • I am trying to get all the checkboxed values of a field group. For my purposes I need a foreach loop to be outside of where I’m calling the variable.

    $disciplines = get_field("form_disciplines");
    $discipline;
    
    if( $disciplines ):
    foreach ($disciplines as $discipline) {					
    $discipline;
    }
    endif;
    
    $output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$location['lat'].'" data-lng="'.$location['lng'].'">
    <p>'.$discipline.'</p>
    </div>';

    If the user had checked multiple boxes, this code only returns the first checkboxed item for each. I need it return all check marked boxes. Any pointers greatly appreciated.

  • You won’t be able to output all of them unless they are in your loop. I’m not sure what your initial $discipline; call is as well as the one within your loop. You’re not doing anything with it there.

    You’ll need something like this:

    
    <?php
    $output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$location['lat'].'" data-lng="'.$location['lng'].'"></div>';
    
    $disciplines = get_field("form_disciplines");
    // $discipline; not sure what this is?
    
    if( $disciplines ):
    foreach ($disciplines as $discipline) {         
        echo '<p>' . $discipline . '</p>';
    }
    endif;
    ?>
    

    Your map marker output doesn’t include any of the code in your loop so you can put it before or after your foreach loop depending on where you need it.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Retrieve multiple checkbox values outside foreach loop’ is closed to new replies.