views:

72

answers:

1

I'm trying to see if a CCK checkbox field is checked.

Is it something like:

if ($node->field_checkbox[0]['value'] = 'checked')

?

Thanks.

A: 

You can easily check the contents of an object (e.g. $node) by installing the Devel module and using dsm($node).

In the case of a CCK checkbox, $node->field_fieldname contains an array with at least one element. Each element corresponds to each checked checkbox for the field, and the value key for the checkbox's element is set to the value you specified in the configuration for the field.

Otherwise, if the checkbox is unchecked, it will not appear as an element within $node->field_fieldname.

However, if there are no checkboxes checked, $node->field_fieldname will still contain one element, but the value key for that element will be unset/set to NULL.

So, let's say you had a field, field_checkbox, with two checkboxes: 1) Foo which has a value of foovalue, and 2) Bar which has a value of barvalue.

To check if Foo is checked, you might do:

foreach ($node->field_checkbox as $checkbox) {
  if ($checkbox['value'] == 'foovalue') {
     return TRUE;
  }
}
Mark Trapp
If don't want to use devel module, print structure via print_r($node->field_checkbox);
Nikit