People often ask how to create a drop down selection menu for some item, they often seem to struggle coming up with some weird coding or attempt with pure html.
osC includes a function tep_draw_pull_down_menu which I will use, this removes much of the coding but requires a correctly formatted multi-dimensional
array, this is the achilles heel for many, preventing them using the function.
The parameters for tep_draw_pull_down_menu are ($name, $values, $default = '', $parameters = '', $required = false)
$name is the id for the field
$values is the array containing the selection data
$default is the default value selected
$parameters optional styling params
$required adds the defined TEXT_FIELD_REQUIRED text if true
For this example I will use code I created for a links categories pull down menu for my
reciprocal links contrib.
Step one: create array & put default values
//category drop-down
$category_array = array();
$category_array[0] = array('id' => '0', 'text' => 'Please Select');
Step two: read data from dBASE for use in array
$category_query = tep_db_query("select category_id, category_name from links_categories where status = 1 order by sort_order, category_name");
Step three: loop through the data, placing it into the selection array ($category_array)
while ($category_values = tep_db_fetch_array($category_query)) {
$category_array[] = array('id' => $category_values['category_id'], 'text' => $category_values['category_name']);
}
Note the use of 'id' & 'text', 'id' contains the values returned on selection, 'text' is what's displayed by the drop down (these can be the same).
Remember this array has its own index, it is not indexed by 'id'.
Step Four: Output the drop down!!
<td align="right" width="100%"><?php echo tep_draw_form('category', tep_href_link('links.php', 'action=catsel'), 'get').'Links Category: ' .
tep_draw_pull_down_menu('select_category', $category_array, $selected_cat, 'onchange="this.form.submit();" rel="nofollow"'); ?>
<noscript><input title="View" name="" type="submit" value="Go" /></noscript></form></td>
In this case the drop down is within its own form (drop_downs must always be part of a form), if you are using this as part of a bigger form you can
remove the form elements.
Note the use of tep_href_link in the form action, this is
critical.
Links Category: is the displayed text adjacent the drop.
'select_category' is the field id.
$category_array we created above.
$selected_cat has the currently selected category (so that shows pre-selected in the drop (the default))
'onchange="this.form.submit();"' a parameter, in this case a bit of Javascript to accept input as soon as change made (just an option).
rel="nofollow" often useful to prevent issues with google (duplicate content) this will prevent validation though, use a canonical tag if that's an
issue (just an option).
<noscript><input title="View" name="" type="submit" value="Go" /></noscript> provide a submit button if scripts are off (just an option)
Finally you must process the data at the receiving page, IE
$selected_cat = ($_GET['select_category'] > 0 ? $_GET['select_category'] : false);
this can be the same page if that's where your form action points.
ADVANCED OPTIONS
You may wish to access the data in the selection array separately, this will be an issue as the array has its own index, so placing the same data in a second
array at the same time you create the selection array is the answer, IE within the while loop add:
$categories[$category_values['category_id']] = $category_values['category_name'];
You can now get any category by its id with
echo $categories[$id];
I think I`ve covered it all there, no doubt issue will arrise though.