PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

Drupal Forms API checkboxes database value retrieve and store

November9

Now lets say you want the checkboxes values to come from the database use the following:

function onthisdate_admin() {
$form['onthisdate_maxdisplay1'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of links'),
'#default_value' => variable_get('onthisdate_maxdisplay1', 3),
'#size' => 2,
'#maxlength' => 2,
'#description' => t("The maximum number of links to display in the block.")
);
$result = pager_query('SELECT n.* FROM {node} n '. $filter['where'] .' ORDER BY n.changed DESC', 10, 0, NULL, $filter['args']);
while ($node = db_fetch_object($result)) {
$nodes[$node->nid] = '';
$form['title'][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid));
}
$form['nodes'] = array('#type' => 'checkboxes', '#options' => $nodes);
return system_settings_form($form);
}

Source: modules/node/node.module (core drupal module) line: 1622 function node_admin_nodes()

Theme the checkboxes and its values coming from the database

function theme_onthisdate_admin($form) {
// Overview table:
$header = array(theme('table_select_header_cell'), t('Title'));
if (isset($form['title']) && is_array($form['title'])) {
foreach (element_children($form['title']) as $key) {
$row = array();
$row[] = drupal_render($form['nodes'][$key]);
$row[] = drupal_render($form['title'][$key]);
$rows[] = $row;
}
}
else {
$rows[] = array(array('data' => t('No posts available.'), 'colspan' => '6'));
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}

Now retrieve the above values after form submission to go something worth while with ‘em.

function onthisdate_admin_submit($form_id, $form_values) {
// Filter out unchecked users
$users = array_filter($form_values['users']);
foreach ($users as $uid => $value) {
db_query("INSERT INTO {test} (testid) VALUES ('%s')", $uid);
}
}

Note: “onthisdate” is the name of my test module. Rename it to your module name. If you are new to module development, click here to get the basic info.

posted under Drupal

Email will not be published

Website example

Your Comment:

 

12,831 spam comments
blocked by
Akismet