Create nested xhtml valid select menu with option groups on the fly


Nested Select menu using "optgroup" ver. 1.00

released: 2006-08-12
This example is about the optgroup element, with this "label" it is possible to create really user friendly select menu's. Think about a menu with several product groups and related categories, with the optgroup element it's possible to group the values like in a nested repeated list. The function creates a complete xhtml valid select menu with all option groups (product groups) and options (categories). Within the onchange event the form form will be submitted by default (leave the $form_name argument empty to disable this feature.

Viewed 10522 times

Rating: script rated with stars
 

Your banner at finalwebsites.com?
use the contact form for further information and prices.

<?php
// First you need some code to get a database result set.
// Split the resultset into two arrays, one for the product groups and one for the categories.
$sql = "SELECT id, parent_id, name, var_name FROM prod_category ORDER BY parent_id, id ASC"; $categories = mysql_query($sql);
while ($obj = mysql_fetch_object($categories)) {
    if (!empty($obj->parent_id)) { // there are options
        $arr_options[$obj->id]['parent'] = $obj->parent_id; // the corresponding key (group id)
        $arr_options[$obj->id]['label'] = $obj->name;
        $arr_options[$obj->id]['value'] = $obj->var_name;
        // this array is build to check later the existings of options against the option groups
        // (to create a valid select menu)
        if (!in_array($obj->parent_id, $arr_check)) $arr_check[] = $obj->parent_id;
    } else { // these are group elements
        $arr_groups[$obj->id] = $obj->name;
    }
}

function mysql_grouped_select_menu($elementName, $group_array, $option_array, $label = "", $current = "", $form_name = "myForm") {
    $select = (!empty($label)) ? "<label for=\"".$elementName."\">".$label."</label>\n" : "";
    $select .= "<select name=\"".$elementName."\" id=\"".$elementName."\"";
    $select .= ($form_name != "") ? " onchange=\"document.".$form_name.".submit()\">\n" : ">\n";
    $select .= "  <option value=\"\">...</option>\n";
    if (empty($_REQUEST[$elementName])) {
        if ($current == "") {
            $curr_val = "";
        } else {
            $curr_val = $current;
        }
    } else {
        $curr_val = $_REQUEST[$elementName];
    }
    foreach ($group_array as $gkey => $gval) {
        $select .= "  <optgroup label=\"".$gval."\">\n";
        foreach ($option_array as $opt_val) {
            if ($opt_val['parent'] == $gkey) {
                $select .= "    <option value=\"".$opt_val['value']."\"";
                $select .= ($opt_val['value'] == $curr_val) ? " selected=\"selected\">" : ">";
                $select .= $opt_val['label']."</option>\n";
            }
        }
        $select .= "  </optgroup>\n";
    }
    $select .= "</select>\n";
    return $select;
}
// example function call
echo "
<form action=\"\" method=\"post\" name=\"myForm\">
  "
.mysql_grouped_select_menu("category", $arr_groups, $arr_options, "Choose a category")."
  <input type=\"submit\" name=\"Submit\" value=\"OK\" />
</form>"
;
?>