flaab Posted October 13, 2007 Share Posted October 13, 2007 Hi =) You see...I need to extract from a table all possible values for an enum field to create a drop-down menu. How can I do that? Thanks!!!!! Link to comment https://forums.phpfreaks.com/topic/73124-getting-enum-field-possible-values-from-database/ Share on other sites More sharing options...
kratsg Posted October 13, 2007 Share Posted October 13, 2007 This can be done using the PHP's describe table feature. It will list the enum field, the next problem is using Regular Expressions with this. Since I'm not the perfect person for helping with Regular Expressions, I checked out php.net and found a function that does just this (with an example for populating dropdown box) function mysql_enum_values($tableName,$fieldName) { $result = mysql_query("DESCRIBE $tableName"); //then loop: while($row = mysql_fetch_array($result)) { //# row is mysql type, in format "int(11) unsigned zerofill" //# or "enum('cheese','salmon')" etc. ereg('^([^ (]+)(\((.+)\))?([ ](.+))?$',$row['Type'],$fieldTypeSplit); //# split type up into array $ret_fieldName = $row['Field']; $fieldType = $fieldTypeSplit[1];// eg 'int' for integer. $fieldFlags = $fieldTypeSplit[5]; // eg 'binary' or 'unsigned zerofill'. $fieldLen = $fieldTypeSplit[3]; // eg 11, or 'cheese','salmon' for enum. if (($fieldType=='enum' || $fieldType=='set') && ($ret_fieldName==$fieldName) ) { $fieldOptions = split("','",substr($fieldLen,1,-1)); return $fieldOptions; } } //if the funciton makes it this far, then it either //did not find an enum/set field type, or it //failed to find the the fieldname, so exit FALSE! return FALSE; } EXAMPLE echo "<SELECT NAME=\"Select\" SIZE='1'>"; foreach($fieldOptions as $tmp) { echo "<OPTION>$tmp"; } Hope this helps :-D Link to comment https://forums.phpfreaks.com/topic/73124-getting-enum-field-possible-values-from-database/#findComment-368769 Share on other sites More sharing options...
flaab Posted October 13, 2007 Author Share Posted October 13, 2007 Got it! Thanks =) Link to comment https://forums.phpfreaks.com/topic/73124-getting-enum-field-possible-values-from-database/#findComment-368848 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.