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

<< lastnext >>

Snippet Name: Build HTML select list from table

Description: This function builds an HTML select list from from data retrieved from a mySQL table.

Comment: (none)

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

<?PHP 
 
FUNCTION html_drop_down ($in_name 
                            ,$in_tablename 
                            ,$in_value_column_name 
                            ,$in_display_column_name 
                            ,$in_other_value = "" 
                            ,$in_other_display = "" 
                            ,$in_selected_value = "" 
                            ,$in_where_clause = "") { 
 
    // build the SQL string 
    $sql = "SELECT " . $in_value_column_name . " col_val," . 
                       $in_display_column_name . " col_disp" . 
           " FROM " . $in_tablename; 
    IF ($in_where_clause) { 
        $sql .= " " . $in_where_clause; 
    } 
    $sql .= " order by col_disp"; 
 
    // connect to mysql, put your specifics here. 
    MYSQL_PCONNECT() or DIE ("problems connecting to Database."); 
 
    // queries the database, be sure to name your database. 
    $result = MYSQL_DB_QUERY("ecomm",$sql); 
 
    // build the select list. 
    IF ($result) { 
       $return_value = "<SELECT NAME=\"$in_name\">"; 
 
       IF ($in_other_display) { 
           $return_value .= "<OPTION VALUE=\"$in_other_value\"> $in_other_display </OPTION>"; 
       } 
 
       WHILE ($rec = MYSQL_FETCH_ARRAY($result)) { 
 
           $return_value .= "<OPTION "; 
 
           IF ($in_selected_value) { 
               IF ($rec["col_val"] == $in_selected_value) { 
                   $return_value .= " SELECTED "; 
               } 
           } 
 
           $return_value .= " VALUE=\"" . $rec["col_val"] . "\"> " . 
                            $rec["col_disp"] . " </OPTION>"; 
        } 
 
        $return_value .= "</select>"; 
    } ELSE { 
        $return_value .= "No data."; 
    } 
 
    MYSQL_FREE_RESULT($result); 
 
    RETURN $return_value; 
 
} 
?> 
 
<? 
/* Here's three examples of how you can use this function */ 
 
/* Simple example */ 
ECHO html_drop_down ("formvar1_prod_type" 
                     ,"prod_types" 
                     ,"prod_type_id" 
                     ,"name"); 
 
/* An example with an extra value in the list, good for N/A, All values for queries etc.. */ 
ECHO html_drop_down ("formvar2_prod_type" 
                    ,"prod_types" 
                    ,"prod_type_id" 
                    ,"name" 
                    ,"-1" 
                    ,"All Types"); 
 
/* An example with a where clause */ 
ECHO html_drop_down ("formvar3_prod_type" 
                    ,"prod_types" 
                    ,"prod_type_id" 
                    ,"name" 
                    ,"-1" 
                    ,"All Types" 
                    ,"3" 
                    ,"WHERE prod_type_id <> 4"); 
 
 
/* Here's the data used in my example, it should work with any table: 
 
mysql> select prod_type_id, name from prod_types; 
+--------------+-------------------+ 
| prod_type_id | name              | 
+--------------+-------------------+ 
|            1 | Mother Boards     | 
|            2 | Graphics Cards    | 
|            3 | Memory            | 
|            4 | Pointing Devices  | 
|            5 | Keyboards         | 
|            6 | Operating Systems | 
+--------------+-------------------+ 
6 rows in set (0.00 sec) 
*/ 
 
?>
 


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 196 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?