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?

    • daryl

    • April 26, 2017 at 4:15 am

    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
    
    • daryl

    • April 26, 2017 at 9:45 am

    Brilliant! Works like a charm. Thank you!

    • solomax

    • December 8, 2017 at 3:44 am

    @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
    
Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.