Support

Account

Home Forums General Issues [Checkbox] How to display all item as check uncheck

Solved

[Checkbox] How to display all item as check uncheck

  • [Checkbox] How to display all item as check uncheck.

    List item A,B,C,D,E.F,G,H
    This post is check A,B,C,D
    How to display all item but show status on check or uncheck ?

    Why I don’t use True/false because it can’t our group.

    I’m so sorry. I’m not good Englist.
    Thank you so much.

  • 
    $values = get_field('checkbox_field_name');
    $field = get_field_object('checkbox_field_name');
    $choices = $field['choices'];
    foreach ($choices as $value => $label) {
        echo $label,': ';
        if (in_array($value, $values)) {
            echo 'checked';
        }
        echo '<br />';
    }
    
  • Thank you very much.

    It’m Work. 😀
    ^_^

  • How to add in function?

  • Hi there

    Thanks for an awesome plugin. I cant get enough 🙂

    I am trying to do something similar to @maxpeerawat @hube2’s solution works for me but only if I set the field to return a VALUE only. I am using BOTH.

    I have tried a few things but have been struggling for hours now. Can you please shed some light or point me in the right direction… I have this so far:

    <?php 
       $values = get_field('features');
       $field = get_field_object('features');
       $choices = $field['choices'];
       foreach ($choices as $value => $label) {
          echo $label,': ';
          if (in_array($value, $values['value'])) {
             echo 'checked';
          }
       }
    ?>

    Regards
    Daryl

  • So the original answer was written before this was an option, in fact, the documentation here for the checkbox does not give what the array looks like and I’ve never used this option. The returned array looks something like this

    
    Array
    (
        [0] => Array
            (
                [value] => 1
                [label] => One
            )
    
        [1] => Array
            (
                [value] => 2
                [label] => Two
            )
    
        [2] => Array
            (
                [value] => 3
                [label] => Three
            )
    
    )
    

    Given that you need to loop through both the choices and the values

    
    
    $values = get_field('checkbox_field_name');
    $field = get_field_object('checkbox_field_name');
    $choices = $field['choices'];
       foreach ($choices as $choice_value => $choice_label) {
          echo $choice_label,': ';
          foreach ($values as $value) {
            if ($value['value'] == $choice_value) {
              echo 'checked';
            }
          }// end foreach $values
       } // end foreach $choices
    
  • Brilliant! Works like a charm. Thank you!

  • @hube2 the code is great but I would like to add an else option for those unchecked.

    a simple if else is not working
    So if checked echo ‘checked’; else echo ‘unchecked’;

    Please show me the light 😉

  • This gets me started. Is there a way to wrap everything in a container div and add an IF statement so it isn’t added if there are no values entered (i.e. nothing is checked)? Newbie here breaking things trying to figure it out.

  • @solomax Did you ever find a solution? I tried using @hube2 code and added a simple else like you were looking for, but it will only echo “checked” on the last selected option. So if I have 4 options, checked options 1,2 and 4. It only adds “checked” to option 4.

  • If you are returning the value and the label in the field settings, it gets complicated. There isn’t an easy way to access the nested array values. We need to set a marker to see if we find it selected.

    
    $values = get_field('checkbox_field_name');
    $field = get_field_object('checkbox_field_name');
    $choices = $field['choices'];
    foreach ($choices as $choice_value => $choice_label) {
        echo $choice_label,': ';
        $found = false;
        foreach ($values as $value) {
          if ($value['value'] == $choice_value) {
            $found = true;
            echo 'checked';
          }
        } // end foreach $values
        if (!$found) {
          echo 'not ckecked';
        }
     } // end foreach $choices
    
  • I have been trying to get this code to work but I keep getting an error “Trying to access array offset on value of type bool on line 110”. My checkbox field name is available_features and it is a sub field in a group name tier. I have tried both codes:

    Version 1:

    <?php
    $tier = get_field('tier');
    $values = get_field($tier['available_features']);
    $field = get_field_object($tier['available_features']); 
    $choices = $field['choices'];  // This is the line 110 that keeps throwing the error ?>
    
    <?php foreach($choices as $choice => $label) : ?>
    <?php foreach ( $values as $value ) : ?>
    <?php if( $value['value'] == $choice ) {		
           $status = 'available';
          }
       else {
           $status = 'unavailable';
       } ?>
    <?php endforeach; ?>
    <li class="<?php echo $status;?>"><?php echo $label;?></li>
    <?php endforeach;?> 

    Version 2:

    <?php
    $tier = get_field('tier');
    $values = get_field('available_features'); // This is the difference in V1 and V2
    $field = get_field_object('available_features'); // This is the difference in V1 and V2
    $choices = $field['choices']; // This is the line 110 that keeps throwing the error ?>
    
    <?php foreach($choices as $choice => $label) : ?>
    <?php foreach ( $values as $value ) : ?>
    <?php if( $value['value'] == $choice ) {		
           $status = 'available';
          }
       else {
           $status = 'unavailable';
       } ?>
    <?php endforeach; ?>
    <li class="<?php echo $status;?>"><?php echo $label;?></li>
    <?php endforeach;?> 

    I have also tried both with “get_sub_field” and “get_sub_field_object”. I keep running into the same error.

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

The topic ‘[Checkbox] How to display all item as check uncheck’ is closed to new replies.