Community Documentation

Forms: Creating a check box select and setting its defaults from a query

Last updated December 14, 2008. Created by choster on November 28, 2007.
Edited by LeeHunter, yurtboy. Log in to edit this page.

What was easy is now hard as I work with the FAPI (though it does seem overall a lot better than by hand).

I have a table that stores user's subscribed content types. And I have a table that stores allowed Content Types users can subscribe to. So by querying the Content type table I get a checkbox list of items. But I also want to set the ones as checked that the user already has chosen.

Here is the form:

function buyctsubs_user_settings() {
//$form = array();
global $user;
/**
* create the node type array with ID and nice names
*
*/
$sql = "SELECT bt.type_id as TypeID, nt.name as Proper FROM {buyctsubs_type} as bt LEFT JOIN {node_type} as nt on nt.type LIKE bt.type_name ";
$result = db_query($sql);
  while ($data = db_fetch_object($result)) {
$vars[$data->TypeID] = $data->Proper; //this builds the form from the table of allowed content types
}

/**
*Need to make a query to get and set users defauls
*This also prevents type from showing up that are not allowed
*/
$sql = "SELECT t.type_id as TypeID, nt.name as Proper
FROM {buyctsubs_type} AS t
LEFT JOIN {buyctsubs_user} AS u ON u.type_id LIKE t.type_id
LEFT JOIN {node_type} as nt on nt.type LIKE t.type_name
WHERE u.user_id = %d";
//echo $sql;
$result = db_query($sql, $user->uid);
  while ($data = db_fetch_object($result)) {
$users_vars[] = $data->TypeID; // this sets the defaults in a way that is needed for below note it differs from above
}

$form['buyctsubs_user_settings'] = array(
     '#type' => 'checkboxes',
     '#title' => t('Users may subscribe these node types'),
     '#options' => $vars, ***HERE IS ONE VARIABLE***
     '#default_value' => $users_vars,   ***HERE IS ANOTHER VARIABLE(see note below)***
     '#description' => t('Choose as many as you want'),
  );

  $form['array_filter'] = array('#type' => 'hidden');
  $form['submit'] = array(
   '#type' => 'submit',
   '#value' => t('Save')
);
if(isset($_POST[submit])){
drupal_set_message(t("Your settings have been saved"));
}
return $form;
//  return system_settings_form($form);
}

That is it, now this form minus the ***HERE IS ONE VARIABLE*** callouts, makes a check list of preselected content types so the user know what they already have chosen and can remove or add to the list. I ran into trouble because I tried using the $users_vars[] = $data->TypeID like the $vars[$data->TypeID] = $data->Proper but after removing the first part of the array and just using the values value and not the name as well it was fine.

About this page

Drupal version
Drupal 5.x

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