3

I use checkboxes in a form to capture a users selection as:

$form['mbo_block_config']['livembo_classes_details'] = array(
                '#type' => 'checkboxes',
                '#title' => t('Class Details'),
                '#description' => t('Choose class details to display.'),
                '#options' => $details,
                '#default_value' => variable_get('livembo_classes_details', array('c_id','c_cd_name')),
                '#required' => TRUE,                
                );

The available options are stored in $details which is an associative array:

$details = array(
          'c_id' => t('Class ID'),
          'c_cd_name' => t('Class Name'),
          'c_cd_description' => t('Class Description'),
          'c_l_name' => t('Location: Name'),
          'c_l_city' => t('Location: City'),
          'c_l_full' => t('Location: Full Address'),
          'c_l_phone' => t('Location: Contact Number'),
          'c_s_FullName' => t('Staff: Name'),     
          'c_start ' => t('Class Start Time'),
          'c_end' => t('Class End Time')
          );

I use variable set variable_set('livembo_classes_details', $edit['livembo_classes_details']); which works fine, however it flattens the array and changes the keys to values as per:

Array ( [0] => c_id [1] => c_l_phone [2] => c_l_name )

How can I retain the key/value format in the array as originally specified? For example:

Array ( [c_id] => t('Class ID') [c_l_phone] => t('Location: Contact Number') [c_l_name] => t('Location: Name') )
1
  • there is something wrong with $edit['livembo_classes_details']. variable_get and variable_set work fine with keyed arrays. Sep 23, 2011 at 21:26

1 Answer 1

4

The problem is not variable_get()/set(). It stores whatever you pass to it as a serialized array.

However, what you are saving are the keys of your array, because that's what is passed from the browser when submitting the form. This is also what is expected in the #default_value so it needs to be like this.

When accessing the information, all you need to do is get the intersection of your $details and the selected keys like this:

$selected_details = array_intersect_key($details, array_flip(variable_get('livembo_classes_details', array('c_id','c_cd_name')));

I would recommend to add a helper function which returns $details if you haven't already, so you can re-use that definition and a second helper function which contains the above code.

2
  • Sorted! So simple, cant believe I was so hung up on it when its more of a html form issue.. Thanks
    – WestieUK
    Sep 25, 2011 at 8:30
  • As a general advice, I strongly recommend to use the dpm() (readable display of arrays/objects) and ddebug_backtrace() (readable backtrace) functions whenever something doesn't work as expected. Add dpm($variable) for anything relevant to see it's content so that you know that you're working with. In case of forms, print $form_state['values']/$edit.
    – Berdir
    Sep 25, 2011 at 8:33

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.