i've been working on some reusable (read: generic) functions lately. one of them displays all the data from a fetched mysql row in a nice table. another one (the topic of this post) also outputs a pretty table, but what's more is that it is your standard edit form too. the only inputs the functions take are the table_name, column_name, and column_value (so i can select * from table_name where column_name=column_value).
so basically the script works through the fetched row, field by field, and depending on what the field's type, flags and length are, it outputs the edit form html accordingly.
i've included the code below. it's not exactly optimised (i'm relatively new to php so i am sure you guys could quickly point out a number of improvements within seconds; is using switch case a better way to go for example?).
Code: Select all
...
$query = "select * from " . $table_name . " where " . $column_name . "='" . $column_value . "'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$field_type = "";
$field_length = "";
$field_flags = "";
$field_data = "";
echo "<table>\n";
for($i = 0; $i < mysql_num_fields($result); $i++)
{
$field_name = mysql_field_name($result, $i) . "</th>";
$field_type = mysql_field_type($result, $i);
$field_length = mysql_field_len($result, $i);
$field_flags = explode(" ", mysql_field_flags($result, $i));
$field_data = $row[$i];
echo "\t<tr>";
echo "<th>" .$field_name. "</th>";
if ($field_type == "int" && in_array("auto_increment", $field_flags))
{
echo "<td>" .$field_data. "</td>";
}
else
{
if($field_type == "int")
{
echo "<td><input type='text' name='" .$field_name. "' size='10' maxlength='" .$field_length. "' value='" .$field_data. "'/></td>";
}
else if($field_type == "string")
{
echo "<td><input type='text' name='" .$field_name. "' size='100' maxlength='" .$field_length. "' value='" .$field_data. "'/></td>";
}
else if($field_type == "blob")
{
echo "<td><textarea name='" .$field_name. "' rows='10' cols='100' wrap='virtual'>" .$field_data. "</textarea></td>";
}
}
echo "</tr>\n";
}
echo "</table>\n";
...note: i thought of using the mysql_fetch_field object, but i prefer the simplicity of the above code. if it's a potential resource hog please let me know which is the better route.
anyhow, what i am wondering is whether or not all this is such a good idea
i've catered for potential auto_incrementing values, in which case the user doesn't have the option to edit the field. also, in my databases, i only ever have to deal with inputs of type string, int and blog (html intput types text, text, and textarea basically). the blobs are always TEXT types in mysql. i can imagine a scenario where my script wouldn't handle other inputs so effectively (binary images etc) so please bear in mind that i am aware of this. also. my primary keys are always auto_incrementing ints, and every other field is always editable.
the whole idea with this specialised input form creation is that i am getting tired of copy pasting and tweaking html for each new edit form that i am required to code in my line of work (and i have had to code plenty), so i am trying to be resourceful and create something reusable to save me time.
thanks.