starting from this code available on phpmyadmin 2.6.1 libraries , file common.lib.php line 1934
Code: Select all
* Extracts ENUM / SET options from a type definition string
*
* @param string The column type definition
*
* @return array The options or
* boolean FALSE in case of an error.
*
* @author rabus
*/
function PMA_getEnumSetOptions($type_def) {
$open = strpos($type_def, '(');
$close = strrpos($type_def, ')');
if (!$open || !$close) {
return FALSE;
}
$options = substr($type_def, $open + 2, $close - $open - 3);
$options = explode('\',\'', $options);
return $options;
} // end of the 'PMA_getEnumSetOptions' function
I made this set of script
Code: Select all
<?php
############################################### RETURNS AVAILABLE
function available_posted_rowed($posted,$rowed){
//if there is a $posted information then return it
if(strlen(trim($posted))>0){
return trim($posted);
}
else{
return trim($rowed);
}
}
############################################### RETURNS SELECTED STRING FOR FORM OPTIONS
function is_selected_option($id_string,$id_posted){
// if the $id_string string and the $id_posted string are identic
if($id_string == $id_posted){
return " selected ";
}
}
############################################### CREATE OPTIONS FROM ENUM DB FIELD
function enum_values_to_options($table,$field,$select_name,$posted,$rowed){
// $table is the table of my db
// $field is the table field i'd like to get the enum values
// $select_name is the name of the select form (html output)
// $posted is the $_POST informations if exists
// $rowed is the current db stored information if exits
$query = "SHOW COLUMNS FROM ".$table." LIKE '".$field."'";
sql_select($query,$results); // this is my db connection and query abstraction
while($line = mysql_fetch_row($results)){
$open = strpos($line['1'], '(');
$close = strrpos($line['1'], ')');
// check if there are a string with () and if it conttains "enum" string at first
if (!$open || !$close || !preg_match("#^enum#",$line['1'])) {
return FALSE;
}
$options_array = substr($line['1'], $open + 2, $close - $open - 3);
$options_array = explode("','", $options_array);
if(count($options_array)>1){
foreach($options_array as $key => $val){
$options .="\n<option value=\"".$val."\" "
.is_selected_option($val,available_posted_rowed($posted,$rowed))
." >".$val."</option>";
}//end foreach
return "\n<select name=\"".$select_name."\">".$options."\n</select>";
$options = "";
} //end if($count)
} //end while
}
?>
<form action="" method="POST">
<?php
// On a CMS context we may have the $row['the_field'] information available
// the available_posted_rowed() function is usefull when using data error control and may have an
// error message before storing the new info, by doing this we don't loose
// the form selections and don't need to start all over again
echo enum_values_to_options("my_table","the_field","the_field",$_POST['the_field'],$row['the_field']);
?>
<input type="submit">
</form>
comments and questions and suggestions and also corrections are WELCOME!