Hey, I just threw a party as I thought I had solved my enum problem when I found a thread on here about how to extract the different enum values into an array..... but it seems I've not figured it out quite yet. (well, not really a party as I just had breakfast instead of just coffee).

Anyhow, I was happy as a goose typing up what I thought would solve my little problem here, but whooopppsie... now what should turn out to be an automatic form just doesnt turn up at all. (now, please dont tell me I should have gotten coffee instead of breakfast cause I'm out of coffee).

Here's the code that SHOULD work in my eyes:

for ($i = 0; $i < $columns; $i++)
{
$fieldtypetest = mysql_field_type($field_list, $i);
$fieldflagtest = explode (" ", mysql_field_flags($field_list, $i));

if(in_array("enum", $fieldflagtest))
  {
  $render_insert .= "<tr><td bgcolor=\"#B0BADA\"><div  align=right class=\"text\">" .mysql_field_name($field_list, $i). ": </div></td>";
  $render_insert .= "<td><select name=" .mysql_field_name($field_list, $i). ">";

while($enumarray = explode("','", mysql_fetch_array($field_list, $i)))
{
  foreach ($enumarray as $enumvalue)
  {
  $render_insert .= "<option>$enumvalue</option>";
  }
}

  $render_insert .= "</select></td></tr>";
  }
}

Now in my head this should create an automatic selection menu containing the enum values to choose from. I have other scripts like this for the other types of columns, and have made it so they should create an automatic tableform, so that the right type of information would get into the right fields regardless of their names.

    4 days later
    if($selected_table != NULL)
    {
    $execute = mysql_query("SELECT * FROM $selected_table", $conn) or die (mysql_error());
    $number_of_fields = mysql_num_fields($execute);
    $field_list = mysql_list_fields($jaxeed, $selected_table, $conn);
    	$columns = mysql_num_fields($field_list);
    
    for ($i = 0; $i < $columns; $i++){
    $fieldtypetest = mysql_field_type($field_list, $i);
    $fieldlengthtest = mysql_field_len($field_list, $i);
    $fieldflagtest = explode (" ", mysql_field_flags($field_list, $i));
    
    if(in_array("enum", $fieldflagtest))
      {
      $render_insert .= "<tr><td bgcolor=\"#B0BADA\"><div align=right class=\"text\">" .mysql_field_name($field_list, $i). ":</div></td>";
      $render_insert .= "<td bgcolor=\"#B0BADA\"><select name=" .mysql_field_name($field_list, $i). "><option>NULL</option>";
    
      $enum_field = mysql_field_name($field_list, $i);
      $select_command = mysql_query("SELECT $enum_field FROM      $selected_table", $conn) or die (mysql_error());
    $fetch_enum_array = mysql_fetch_row($select_command);
    $enumarray = explode("','",preg_replace("/(enum|set)\('(.+?)'\)/","\\2",$fetch_enum_array));
    
      foreach ($enumarray as $enumvalue)
        {
        $render_insert .= "<option>$enumvalue</option>";
        }
      $render_insert .= "</select></td></tr>";
      }
    }
    

    This is my second try. As I understood it $enumarray should become an array containing all the different enum values, if the column is recongised to have an enum flag. Thereby the "foreach" loop should break the array down and for each look put one value as $enumvalue that would thereby become another "option" in the "select" form.

    Unfortunately the only option visable in the form is the NULL and an option saying "Array". I've added the NULL cause an enum can either be NULL or one of the values.

    If my code is completely wack, and this should be solved in a totally different way, please tell me. I'm only interested in the script finding out if a column is enum or not, and then list the values as options in a select form. I thank anyone who can give me some input on this.

      a year later
      #DB CONNECT STUFF HERE
      
      $query = mysql_query("SHOW COLUMNS FROM table_name LIKE 'enum_column_name'");
      
      $result=mysql_query($query);
      if(mysql_num_rows($result)>0)
      {
      $row=mysql_fetch_row($result);
      $options=explode("','",preg_replace("/(enum|set)\('(.+?)'\)/","\\2",$row[1]));
      
      #$options=sort($options); #UNCOMMENT THIS LINE TO SORT THE ARRAY
      }
      

      Now $options is an array with all possible values

      echo "<select>";
      
      for ($i=0;i<count($options),i++)
      {
      echo "<option name=\"options\" value=\"".$options[$i]."\">".$options[i]."</option><br/>";
      }
      
      echo "</select>";
      

      Hope it helps.

        Write a Reply...