I've seen questions like this posted here asking about getting all possible values for an enum field. I created a function that will do just that, and return the values in either a select menu or radio button group.
I just thought I'd share it with the good folks who use this site. If any of you have comments, suggestions, or find bugs, please post them here. Also, if you find it useful, let me know.
/*
function name:EnumList
description:takes a table column of type enum and returns the possible values
in select menu or radio button list
# of parameters:6 [3 optional]
return value: none
parameters passed:
table - table that the column is in
columnName - name of enum column in table
listName - the HTML form name to assign to the list item
selected - the default value, if it exists
listType - menu or radio
css - CSS class name if exists
*/
function EnumList($table,$columnName,$listName,$selected=0,$listType='menu',$css='none')
{
$Query = "SHOW COLUMNS FROM " . $table . " LIKE '" . $columnName . "'";
//print "QUERY: " . $Query . "<br>\n";
$Result = mysql_query($Query);
if($Result){
$enumString = mysql_result($Result,0,'Type');
} else {
print "No items found!";
}
$enumArray = explode(",", str_replace("'", "",preg_replace("/enum\((.+)\)/", "\\1", $enumString)));
if($listType == 'menu'){
print "<select name=\"" . $listName . "\"";
if($css != 'none'){ print " class=\"" . $css . "\""; }
print ">\n";
foreach($enumArray as $key => $value){
print "<option value=\"" . $value . "\"";
if($value == $selected){ print " selected"; }
print ">" . $value . "</option>\n";
}
print "</select>\n";
} else if($listType == 'radio'){
foreach($enumArray as $key => $value){
print "<input type=\"radio\" name=\"" . $listName . "\" value=\"" . $value . "\"";
if($value == $selected){ print " checked"; }
print ">" . $value . "\n";
}
}
}//END function EnumList
you call it like so:
//called with only the 3 required params
//also assumes connection to DB already established.
EnumList('tableName','columnName','fieldName');
questions, comments, wisecracks...?