Community Documentation

Storing checkboxes in a single CCK field as comma separated values for performance reasons

Last updated May 3, 2010. Created by giorgio79 on April 27, 2010.
Log in to edit this page.

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'])
);
  }

}

Comments

where should i put the

where should i put the mymodulename_form_alter function? Do i need to create my own custom module first?

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Designers/themers, Programmers, Site administrators

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here