If you want to get all possible values for an ENUM column, you should use: SHOW COLUMNS FROM table_name LIKE enum_column_name and parse the ENUM definition in the second column.
i had to think about this for for a sec. try this. remember, $enum would actually be the text from the query. i’m just assigning what you showed for this example:
$enum = "enum('Equipment','Set','Show')";
// Strip "enum('" off the front
// and "')" off the end
$enum = substr($enum, 6, -2);
// Split the rest up on ','
// $values is an array of the possible values
$values = explode("','", $enum);
then just loop through $values to make the <select> list.
now, that explode() part wouldn’t work correctly if one of your actual ENUM values had a single quote on both sides of a comma. but i don’t think that’s likely.
also, if you have any single quotes in the ENUM values, they will be doubled. you could fix that by using this first:
Instead, I used str_replace() twice for cleaning up the ENUM string before putting it into an array. I’m sure that there are smarter ways of getting the same results, but it works nicely.
this is the code that I used:
<?php
// get ENUM values String from specified DB table column using my Database Class
// (using substituted table and col names.)
$DB = new Database();
$sql = "SHOW COLUMNS FROM tbl_the_table LIKE 'category'";
$DB->get_data($sql);
while($DB->row = mysql_fetch_array($DB->result_id)) {
$type = $DB->row["Type"];
}
// format the values
// $type currently has the value of: enum('Equipment','Set','Show')
// ouput will be: Equipment','Set','Show')
$output = str_replace("enum('", "", $type);
// $output will now be: Equipment','Set','Show
$output = str_replace("')", "", $output);
// array $results contains the ENUM values
$results = explode("','", $output);
// create HTML select object
echo"<select name=\\"the_name\\">\
";
// now output the array items as HTML Options
for($i = 0; $i < count($results); $i++) {
echo "<option value=\\"$results[$i]\\">$results[$i]</option>\
";
}
// close HTML select object
echo"</select>";
?>