Skip to content

Instantly share code, notes, and snippets.

@jfensign
Created April 6, 2012 21:35
Show Gist options
  • Save jfensign/2323120 to your computer and use it in GitHub Desktop.
Save jfensign/2323120 to your computer and use it in GitHub Desktop.
Generate forms quickly for testing (Do not use in production)
<?php
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS)
or die(mysql_error());
mysql_select_db(DB);
//SQL
$sql = "SHOW COLUMNS FROM TABLENAME";
//Query
$query = mysql_query($sql)
or die(mysql_query());
//results
while($result = mysql_fetch_object($query)) {
$queryResult[] = $result;
}
?>
<form name="form" enctype="multipart/form-data" method="post" action="url">
<fieldset>
<h4 class="admin_form_top">Create a Project</h4>
<div class="admin_form_element_wrap">
<?php
//iterate through results and conditionally
//set input type based on MySQL datatype
foreach( (array) $queryResult as $key => $obj ) {
//ignore id and image fields
if($obj->Field != "id" && $obj->Field != "image") {
$field = strtolower($obj->Field);
$label = ucwords(str_replace("_", " ", $field));
if(substr($obj->Type, 0, 7) != "tinyint") {
echo "<br /><label for='{$field}'>$label</label><br />";
}
elseif(substr($obj->Type, 0, 7) === "varchar") {
echo "<input type='text' name='{$field}' />";
}
elseif($obj->Type === "text") {
echo "<textarea name='{$field}'></textarea><br />";
}
elseif(substr($obj->Type, 0, 7) === "tinyint") {
echo '<div class="checkbox_wrap">';
echo "<br /><label for='{$field}'>$label</label>";
echo "<br /><input type='checkbox' name='{$obj->Field}' value='0' />";
echo "</div>";
}
}
if($obj->Field == "image") {
echo "<br /><label for='image'>Image</label><br />";
echo "<input type='file' name='image' /><br />";
}
}
?>
<br />
<input type="submit" name="submit" value="Submit" />
</div>
</fieldset>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment