Support

Account

Home Forums ACF PRO output value from checkbox field into table

Solved

output value from checkbox field into table

  • I’m trying to build a table using ACF checkbox fields.

    Each item might have 1, 2, 3 or 4 colours. I want to loop through each item (they are in a repeater field) and output “yes” to the corresponding table cell.

    Output is set to array.

    This is my current code (not working).

    
    <div class=”table-responsive”>
    <table class=”table table-bordered”>
    <thead>
    <tr>
    <th class=”table__title” scope=”col”>Product</th>
    <th class=”table__col” scope=”col”>Yellow</th>
    <th class=”table__col” scope=”col”>Red</th>
    <th class=”table__col” scope=”col”>Blue</th>
    <th class=”table__col” scope=”col”>Green</th>
    </tr>
    </thead>
    <tbody>
    
    <?php while( have_rows(‘matrix’) ): the_row();
    
    // Load sub field value.
    $title = get_sub_field(‘title’);
    $colours = get_sub_field(‘colours’);
    ?>
    
    <tr>
    <td><?php echo $title; ?></td>
    
    <td>
    <?php if($colours == ‘yellow’) { ?>
    YES
    <?php } ?>
    </td>
    <td>
    <?php if($colours == ‘red’) { ?>
    YES
    <?php } ?>
    </td>
    
    <td>
    <?php if($colours == ‘blue’) { ?>
    YES
    <?php } ?>
    </td>
    <td>
    <?php if($colours == ‘green’) { ?>
    YES
    <?php } ?>
    </td>
    
    </tr>
    
    <?php endwhile; ?>
    
    </tbody>
    </table>
    </div>
    
  • Well, if the output is set to array you can’t just do if($colours == 'blue'), you need to either loop through the array and print the results accordingly or you need to access the array items directly. How to do this is explained in the documentation. But apparently you don’t even need the array output, you could just output the value since you’re not doing anything with the labels.

    I guess for you the example under “Conditional logic” in the documentation would be appropriate:

    <td>
    <?php if($colours && in_array('yellow',$colours) { ?>
    YES
    <?php } ?>
    </td>
    <td>
    <?php if($colours && in_array('red',$colours) { ?>
    YES
    <?php } ?>
    </td>
    
    <td>
    <?php if($colours && in_array('blue',$colours) { ?>
    YES
    <?php } ?>
    </td>
    <td>
    <?php if($colours && in_array('green',$colours) { ?>
    YES
    <?php } ?>
    </td>

    (not tested)

  • Yes, according to the documentation that should work. This method does indeed work when the checkbox field is a primary field. I am attempting to extract the checkbox values within a repeater (parent) field.

    This method does work when the checkbox is within a repeater field.

  • Thanks for pointing me right direction. Return format is set to both because the data is used in another query.

    I rewrote to loop through the array, data is now showing.

    <td>
      <?php foreach ($colors as $color): ?>
        <?php if($color['value'] == 'red') { ?>
          <span>✔</span> // html checkmark
        <?php } ?>
      <?php endforeach; ?>
    </td>
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.