Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 PHP Code: Build select menu from database Jump to:  
Category: >> PHP Code >> Build select menu from database Bookmark and Share

<< lastnext >>

Snippet Name: Build select menu from database

Description: This small function is used to build a select menu with the values from
a database table. Create the SQL statement outside the function with two values, one for the (option) value and one for the (option) label. The function is completely dynamic, you can change the name for the html elements (label and select) and you can use all tables you like.

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

<?PHP 
 
// example sql statement 
$sql = "SELECT col_for_value, col_for_label FROM some_table"; 
 
FUNCTION database_select($tbl_value, $tbl_label, $select_name, $label) { 
    GLOBAL $sql; 
    $result = MYSQL_QUERY($sql); 
    $menu = "<label for=\"".$select_name."\">".$label."</label>\n"; 
    $menu .= "<select name=\"".$select_name."\">\n"; 
    $menu .= "  <option value=\"\""; 
    $menu .= (!ISSET($_REQUEST[$select_name])) ? " selected" : ""; 
    $menu .= ">...\n"; 
    WHILE ($obj = MYSQL_FETCH_OBJECT($result)) { 
        $menu .= "  <option value=\"".$obj->$tbl_value."\""; 
        $menu .= (ISSET($_REQUEST[$select_name]) && $obj->$tbl_value == $_REQUEST[$select_name]) ? " selected" : ""; 
        $menu .= ">".$obj->$tbl_label."\n"; 
    } 
    $menu .= "</select>\n"; 
    MYSQL_FREE_RESULT($result); 
    RETURN $menu; 
} 
 
// example: 
// use the col names from the table too... 
ECHO database_select("col_for_value", "col_for_label", "my_select", "Select from:"); 
 
/* example output 
 
<label for="my_select">Select from:</label> 
<select name="my_select"> 
  <option value="" selected>... 
  <option value="val_1">value 1 
  <option value="val_2">value 2 
  ... 
</select> 
*/ 
 
?>


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 208 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?