Php Programming Code Examples Php > Database Related Code Examples PHP form-class generator PHP form-class generator <?php /* CRITICAL NOTE!!! We MUST be guaranteed table structure does NOT change while we are using this script It can change after we have generated our PHP code, but not while we are between steps B and C, in particular */ if ( !isset($db) || !isset($table) ) { echo "<form action=$PHP_SELF>"; echo "DB: <input name=db><br> Table: <input name=table><br> <input type=submit value=Go></form>"; } else { if ( $ready ) { // print_r($_POST); ob_start(); echo '<?PHP class Form_'.$db.'_'.$table.' { var $TEXTAREA_ROWS=6; var $TEXTAREA_COLS=40; '; $fields = mysql_list_fields($db,$table); echo 'var $renderStyle=array('; $n=0; for ( $i=0; $i < mysql_num_fields($fields); $i++ ) { if ( isset($_REQUEST["enable$i"]) ) { echo ($n++==0?'':',').($n % 5 ==1?' ':'')."'".mysql_field_name($fields,$i).'\'=>'."'".$_POST["represent$i"]."'"; } } echo '); ';//close the array() echo 'var $labels=array('; $n=0; for ( $i=0; $i < mysql_num_fields($fields); $i++ ) { if ( isset($_REQUEST["enable$i"]) ) { echo ($n++==0?'':',').($n % 3 ==1?' ':'')."'".mysql_field_name($fields,$i).'\'=>'."'".mysql_field_name($fields,$i)."'"; } } echo '); '; /* END INITIALIZATION ARRAYS */ echo ' function generateForm ( $source="" ) { $fields = array_keys($this->renderStyle); if ( $source == \'request\' || $source == \'input\' || $source ==\'form\' ) { foreach ( $fields as $n=>$f) $$f=$_REQUEST[$f]; } else if ( !empty($source) ) { //$source should be a mySQL string $query = mysql_query($source); $data = mysql_fetch_array($query); //Begin conversion from DB row to our form variables foreach ( $fields as $n=>$f) $$f=$data[$f]; } echo "<table><form action=$_SERVER[PHP_SELF] method=post>"; echo "<input type=hidden name=submitted value=true> <input type=hidden name=process value=$_REQUEST[process]>"; //Time to render the form itself, now that we have the variables; foreach ( $fields as $n=>$f ) { echo "<tr><td>".$this->labels[$f]."</td><td>"; switch($this->renderStyle[$f]) { case \'textarea\': echo "<textarea name=$f rows=TEXTAREA_ROWS cols=TEXTAREA_COLS>".$$f."</textarea>"; break; case \'input\': default: echo "<input type=text name=$f value=\"".$$f."\">"; } echo "</td></tr>"; } echo "<tr><td colspan=2><input type=submit value=\\"Submit\\"></td></tr></form></table>"; } function verifyForm () { foreach ( $this->renderStyle as $f=>$r) if ( empty($_REQUEST[$f]) ) return false; //At least one empty element return true; } function getSQL_setClause () { $c=0; $ret=""; foreach ($this->renderStyle as $f=>$r) { if ( $c++ > 0 ) $ret .= ","; $ret .= "$f=\'$_REQUEST[$f]\'"; } return $ret; } function getSQL_updateRow ( $whereClause) { $sql = "update '.$table.' set " .$this->getSQL_setClause() . " where $whereClause"; return $sql; } function getSQL_insertRow () { $sql = "insert into '.$table.' set " . $this->getSQL_setClause(); return $sql; } } //End Class ?>'; $output=ob_get_contents(); ob_end_clean(); //echo "<hr>$output<hr>"; highlight_string($output); } else { echo "<form action=$PHP_SELF method=post>"; echo "<input type=hidden name=db value=$db><input type=hidden name=table value=$table>"; echo "<input type=hidden name=ready value=true>"; $fields = mysql_list_fields($db,$table); echo "<table>"; for ( $i=0; $i < mysql_num_fields($fields); $i++ ) { switch ( mysql_field_type($fields, $i) ) { case 'int': case 'bigint': case 'mediumint': $choices = array("input"); break; case 'varchar': $choices = array("input"); break; case 'blob': case 'text': $choices = array("textarea", "input");break; default: $choices=array("input"); } echo "<tr>"; echo "<td><input type=checkbox name=enable$i value=true title=\"Enable this column\" checked></td>"; echo "<td>".mysql_field_name($fields, $i) . "-<b>" . mysql_field_type($fields,$i) . "</b> " . mysql_field_len($fields,$i) ." (" . mysql_field_flags($fields,$i).")" . "</td>\n"; foreach ( $choices as $n=>$k ) { echo "<td><input type=radio name=represent$i value=$k " . ($n==0?'checked':'').">$k</td>"; } echo "</tr>"; } echo "</table>"; echo "<input type=submit value=\"Generate PHP\"></form>"; } } function ob_pre ( $buffer ) { return "<pre>".$buffer."</pre>"; } ?>