faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 23 people (78%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How do I display all ENUM values defined in my database on a Pulldown menu?

Aug 7th, 2001 10:56
Jared Smith, Luis Delgado, http://www.phpwizard.net/projects/phpMyAdmin/


This is something I use, modified from phpMyAdmin code:

<pre>
//////////////////////////////////////////////////
// create a select box to select a category
//////////////////////////////////////////////////
function category_select($what, $where, $multiple ) {
    // Category field is an ENUM, so we have to parse it out
    $table_def = mysql_query("SHOW FIELDS FROM knowledge");
    if (!$table_def) { die ("SQL category selection error: ".mysql_error
()); }
    
    // create a selectbox
    // if 'multiple' is greater than 1, it's a multiple-selectbox
    echo "<SELECT NAME=\"$what\" size=$multiple"; if ($multiple>1) { 
echo " MULTIPLE"; } echo ">\n";
    
    // grab the relevant record
    for($i=0;$i<mysql_num_rows($table_def);$i++) {
        $row_table_def = mysql_fetch_array($table_def);
        if( (strstr($row_table_def["Type"], "enum")) && ($row_table_def
["Field"]== "knowledge_category") ) {
            // replace all extraneous characters with ""
            $set = str_replace("enum(", "", $row_table_def["Type"]);
            $set = str_replace("'", "", $set);
            $set = str_replace(")", "", $set);
            $set = str_replace("\\", "", $set);
            $set = stripcslashes($set);
            echo $set;
            // blow it up
            $set = explode(",", $set);
            // for each category, write an HTML option
            for($j=0; $j<count($set); $j++) {
                echo '    <OPTION VALUE="'.$set[$j].'"';
                // selecting the one that matches
                if   ($set[$j]==$where) {
                    echo " SELECTED";
                }
                echo ">".$set[$j]."</OPTION>\n";
            }
        }
    }
    echo "</SELECT>\n";
}
</pre>