Simple HTML <select> using custom database tables.
Inspired by http://drupal.org/node/76605 here's some starter code for a block snippet to produce such a selector.
By way of example, this sample will provide a select choice of the various node types in your system. Note, it doesn't get the human readable name using the Drupal API, just the raw "type" field from the database as this sample is to show how to get various information from the database and produce a selector. The final SQL used here is down to your database and your design.
In order to demonstrate the _submit() handler, we assume you have another table where you are going to store the selections your users choose. For this example we assume a simplist custom table you have created yourself called my_custom_table which consists of two fields, uid and selected. This part you will need to substitute your own code depending on your requirements.
To use this code, create a block, cut'n'paste the following code (adjusted for your needs) and set the input filter to PHP
<?php
function my_selector($default = NULL) {
// Prepare the <select> options array
$options = array();
// Write some SQL to get the options you desire and run thequery
$sql = "SELECT DISTINCT type FROM {node} ORDER BY type";
$r = db_query($sql);
// Get each option and populate the options array.
while ($row = db_fetch_array($r)) {
$options[$row['type']] = $row['type'];
}
// Start making the FormAPI form.
$form['my_select'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('My select title'),
);
// Here, we can choose a default value. It may be that you
// can read from your custom table to see if the user has
// made a previous selection and set this appropiatly. This
// is left as an exercise for the reader.
if (!is_null($default)) {
$form['my_select']['#default_value'] = $default;
}
// Add the form submit button.
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Please select'),
);
// Return the form to Drupal's FormAPI.
return $form;
}
// This function is called when a slection is submitted.
function my_selector_submit($form_id, $form) {
global $user;
// The reader should modify the following code to suit their needs.
// This is just provided by way of example. As more of a hint, the
// user may have already made a selection and maybe changing it in which
// case you would need to update. Those familiar with MySQL will like
// that fact that a REPLACE INTO would be more suitable here. However,
// Postgres doesn't support REPLACE INTO. However, if this is just for
// your site you can code it how you like. Only when creating modules
// do you need to be aware of database vendor issues.
// As stated, it's left to the reader to customise this section.
$sql = "INSERT INTO {my_custom_table} (uid, selected) VALUES (%d, '%s')";
db_query($sql, $user->uid, $form['my_select']);
// Provide a message to the site user.
drupal_set_message(t('Thank you for selecting @select', array('@select' => $form['my_select'])));
}
return drupal_get_form('my_selector');
?>btw, the above is a basic starting point. You are advised to see the handbook section on writing secure code
