Storing checkboxes in a single CCK field as comma separated values for performance reasons
Last updated on
30 April 2025
I was unhappy with the way CCK stores multi value options widgets, as for each value it adds a separate row in a content field table. I had a content type where 100 selections (checkboxes) were needed and that meant 100 table rows per node created by CCK. As the table would get larger this might be a problem with all the Joins Views and CCK makes...
In order to save on space and hosting fees I decided to store the selected values in a single text field as a comma separated value.
Here is the code for the
Computed Code:
// Remove "0" Values, do not store unselected keys
$new_array_without_nulls = array_filter($node->completed_services);
// Store checked boxes
$node_field[0]['value'] = implode(',',$new_array_without_nulls);
And in hook form alter, add the checkboxes to your form:
function mymodulename_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "my_node_form")
{
$nodes = db_query("CUSTOM SQL TO GET CHECKBOX KEYS AND VALUES");
while ($node = db_fetch_object($nodes)) {
$checkboxarray[$node->nid] = $node->title; // In my case I used as node ids as keys and node titles as values from content type
}
$form['my_checkboxes'] = array
(
'#title' => "My Checkboxes",
'#type' => 'checkboxes',
'#options' => $checkboxarray,
'#default_value' => '#default_value' => explode(",",$form['#node']->field_computed['0']['value'])
);
}
}
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion