I have a 'List (text)' field set up with five options displayed as a series of checkboxes.

I would like the last of these checkboxes to control the visibility of a button in the node template.

<?php if($node->field_the_checkbox_field['und'][0]['value'] == 'key_of_checkbox'): ?>
    // do stuff
<?php endif; ?>

The problem with the above is that it only works when this checkbox is the only one selected, otherwise it is no longer the '[0]' result.

How could I go about checking all of the potential five checkboxes for my result?



I thought I'd found a horribly verbose workaround by checking every offset one by one like so:

<?php if($node->field_the_checkbox_field['und'][0]['value'] == 'key_of_checkbox'
    || $node->field_the_checkbox_field['und'][1]['value'] == 'key_of_checkbox'
    || $node->field_the_checkbox_field['und'][2]['value'] == 'key_of_checkbox'): ?>
        // do stuff
<?php endif; ?>

etc... but, now I get an 'undefined offset' error for each offset that doesn't exist, ie. not all of the boxes will ever be checked, and so there are never usually five results, meaning that when only two of these checkboxes are checked offsets 2 - 4 don't exist. PHP doesn't like that, apparently.

I'm wondering if my problem here is that I should be using five boolean checkboxes, which at least will always return a result one way or another. That seemed messy when I was planning this out, but now it's looking ever more inviting!


Since I'm short on time, I decided to go with my thoughts above and base my conditionals on single points of data which can either return me a 'true' or 'false' status. My PHP (as you can probably see above) is crappy to say the least - far too crappy to work out how to get around this one. I'd be interested if anyone has any ideas regarding this though!

Thanks for reading.

EXTRA EDITS for clarity and grammar

1 Answer 1


My php isn't the best, but I think this should work...

You could try using a foreach loop, and using a condition similar to this:

<?php foreach ($node->field_the_checkbox_field['und'] as $thisfield) {
    if($thisfield['value'] == 'key_of_checkbox'):
   // do stuff
} ?>
  • Thanks Geoff, I don't have time to check this out right now so I wont mark it as the answer, but when I do finally find time, I'll be sure to report back!
    – PUncle
    May 15, 2014 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.