Semiauto Select/Option code


New member
I have a web page generated from a sql db - calendar. I got tired of having to update the code for every any minor change with Enum columns in the db. So...

  # Func: DB, Table, Column, prev.selected, prefix if joined tables
  # joined tables: tabA data for web display, 
  #  tabB data for admin, how paid, contact info, notifications

  function do_enum ($db, $table, $ecol, $seled, $ab)
    # get the list of unums
    $q1 = mysqli_query ($db, "show columns from $table where field = '$ecol';");
    $u1 = mysqli_fetch_row ($q1);
    # convert to an array
    $enumarr = preg_split ("/\,/", (str_replace ("'", "", substr($u1[1], 5, -1))));
    # which one was selected?
    foreach ($enumarr as $l)
      if ("$l" == "$seled") break;
    # generate code
    echo "<select name=$ab$ecol>\n";
    foreach ($enumarr as $item)
      $seled = ($sel == $n) ? "selected":""; # which one gets "selectED"
      echo "<option value=$item $seled>$item\n";
    echo "</select>";

do_enum ($db, 'table', 'column', 'lastseled', 'b');

The function call is: $DB, tableName, columnName, item4selected, multitable.
The function queries the DB table and gets the Enums: item1, item2, ..itemN
and creates the <select name=column ... $sel($n) = (int)enum item to set "selected"
on the <option line. 'b' is (on my site "a" or "b" - joined tables IE aid = bid, aname, bnote[/code]
Last edited: