PHP Tutorial: Put MySQL ENUM values into drop down select box

Comments 8 Standard

If you have an ENUM field in your table schema then you don’t want to go through the hassle of re-writing out all of the different ENUM values when you do an online form or CMS update system. This piece of code will take all of your ENUM values and put them into a select box so the choices displayed in the drop down are always up to date.

$table_name = "your_table_name"
$column_name = "field_with_enum_options";

echo "<select name=\"$column_name\">";
$result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'")
or die (mysql_error());

$row = mysql_fetch_array($result);
$enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

foreach($enumList as $value)
    echo "<option value=\"$value\">$value</option>";

echo "</select>";
enumList

Want to use a function instead? Try this:

function enumDropdown($table_name, $column_name, $echo = false)
{
   $selectDropdown = "<select name=\"$column_name\">";
   $result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
       WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'")
   or die (mysql_error());

    $row = mysql_fetch_array($result);
    $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

    foreach($enumList as $value)
         $selectDropdown .= "<option value=\"$value\">$value</option>";

    $selectDropdown .= "</select>";

    if ($echo)
        echo $selectDropdown;

    return $selectDropdown;
}
enumList

DOJO Tutorial: Clear All Data From The Grid

Comments 3 Standard

Unforutnately if you try to call the startup() function again (the logical way to create your grid all over again…come on now DOJO) you’ll get a the error: “Tried to register widget with id==grid but that id is already registered”.

For some reason DOJO wasn’t smart enough to clear out the old IDs associated with the grid. So the only way to clear all of the data out of the grid cells is to update the grid’s data store. Let’s say you have a grid called myGrid and you wanted a button to clear it. You could use the following function:

//this assumes myGrid is global
function clearMyGrid()
{
var emptyCells = { items: "" };
var emptyStore = new dojo.data.ItemFileWriteStore({data: emptyCells});
myGrid.setStore(emptyStore);
}

Now let’s say you wanted to make this function more generic so it will apply to any grid you want to clear. Update it to look like this instead:

//this allows you to clear any grid as long as you pass an instance of it to the function
function clearGrid(gridInstance)
{
    var emptyCells = { items: [] };
    var emptyStore = new dojo.data.ItemFileWriteStore({data: emptyCells});
    gridInstance.setStore(emptyStore);
}