phpFormGenerator is a class file which dynamically creates data input form and its handler PHP file on thefly from a given MySQL table or a query. That means you need not to work hard for generating Data Input Forms anymore.
This class is extremely helpful for rapid Application Development. Use this class for production level.
phpFormGen
<?php
/*******************************************************************************************
*
* Class Name : phpFormGenerator
* Version : 1.1
* Written By : Hasin Hayder
* Start Date : 19th January, 2005
* Copyright : 2005 by Hasin Hayder (www.hasinme.info)
*
********************************************************************************************
* This class generates input forms for a mysql table on the fly.
* Just supply the necessary information and thats all.
* Released under GPL
*
********************************************************************************************
* Contact : My site is http://www.hasinme.info (www.hasinme.tk)
* My email address is countdraculla@gmail.com
* IDE Used : Zend Studio (www.zend.com) and PHP Designer 2005 (www.mpsoftware.dk)
* Thirdparty : I use a CSS file (style.css) and some images which originally comes with
InvisionPowerBoard version 1.x
* Future Plan: Support for Blob Field and MSSQL Server, PostgreSQL Server
*
*/
class phpFormGenerator
{
var $mysql_resource ;
var $fields = array();
var $cellpad ;
var $cellspace ;
var $border ;
var $width ;
var $modified_width ;
var $table_name ;
var $db_name ;
var $db_host ;
var $db_pwd ;
var $db_user ;
var $key_field ;
function generateInsert ()
{
$this -> initialize ();
//Now start Form Generation
$field_count = mysql_num_fields ( $this -> mysql_resource );
$i = 0 ;
while ( $i < $field_count )
{
$field = mysql_fetch_field ( $this -> mysql_resource );
$this -> fields [ $i ] = ucfirst ( $field -> name );
$i ++;
}
$query_string = "show fields from $this -> db_name ." . $this -> table_name ;
mysql_connect ( $this -> db_host , $this -> db_user , $this -> db_pwd );
$result = mysql_query ( $query_string );
//Hedear Block Genaration
$fp .= "<HTML>\n<HEAD>" ;
$fp .= "<link href='style.css' rel='stylesheet' type='text/css'>\n" ;
$fp .= "</HEAD>\n" ;
$fp .= $this -> readStyle ();
$fp .= "<BODY>\n<BR>\n" ;
$fp .= "\n<FORM name = 'form1' method = 'POST' action = ''>" ;
$fp .= "\n" . "<table width=' $this -> modified_width ' border=' $this -> border ' cellspacing=' $this -> cellspace ' cellpadding=' $this -> cellpad '>" ;
//Header Block Generation End
while ( $rows = mysql_fetch_row ( $result ))
{
$field_name = $rows [ 0 ];
if ( in_array ( ucfirst ( $field_name ), $this -> fields ))
{
$fp .= "\n" . "<TR>" ;
$fp .= "\n" . "<TD class='row4'> <div align='left'><span class='postdetails'>" ;
$fp .= "\n<B> " . $field_name . "</B>" ;
$fp .= "\n" . "</span></div></TD>" ;
$handler .= "\n" . "$" . " $field_name = " . "$" . "HTTP_POST_VARS[' $field_name '];" ;
$field_data = $rows [ 1 ];
$field_type = $this -> getTypes ( $field_data );
$field_size = $this -> getSize ( $field_data );
$field_definition .= $field_name . "," ;
$data_definition .= "'$" . $field_name . "'," ;
//Size Calculation and Field generation
$field_space = ( $field_size > 50 )? 60 : $field_size + 5 ;
//End Size Calculation
$fp .= "\n" . "<TD class='row4'> <div align='left'><span class='postdetails'>" ;
if ( ceil ( $field_size )> 1 && strtolower ( $field_type ) == "varchar" )
{
$fp .= "\n" . "<input type ='text' name = ' $field_name ' size = ' $field_space ' maxlength = ' $field_size ' value = " . '"' . "<? echo $" . " $field_name ; ?>" . '"' . ">" ;
}
else if( ceil ( $field_size )== 1 && strtolower ( $field_type ) == "varchar" )
{
$fp .= "\n" . "<input type= 'checkbox' name = ' $field_name ' value = 'true'> Set " ;
}
else if( strtolower ( $field_type ) == "blob" )
{
$fp .= "\n" . "<input type= 'text' name = ' $field_name ' size = '60'> value = " . '"' . "<? echo $" . " $field_name ; ?>" . '"' . ">" ;
}
else if( strtolower ( $field_type ) == "int" )
{
$fp .= "\n" . "<input type = 'text' name = ' $field_name ' size = ' $field_space +5' maxlength = ' $field_size ' value = " . '"' . "<? echo $" . " $field_name ; ?>" . '"' . ">" ;
}
else if( strtolower ( $field_type ) == "date" )
{
$fp .= "\n" . "<input type= 'text' name = ' $field_name ' size = ' $field_space ' maxlength = '10' value = " . '"' . "<? echo $" . " $field_name ; ?>" . '"' . ">" ;
}
else if( ceil ( $field_size )> 1 && strtolower ( $field_type ) == "char" )
{
$fp .= "\n" . "<input type= 'text' name = ' $field_name ' size = ' $field_space ' maxlength = ' $field_size ' value = " . '"' . "<? echo $" . " $field_name ; ?>" . '"' . ">" ;
}
else if( ceil ( $field_size )== 1 && strtolower ( $field_type ) == "char" )
{
$fp .= "\n" . "<input type = 'checkbox' name = ' $field_name ' value = 'true'> Set" ;
}
else if( strtolower ( $field_type ) == "text" )
{
$fp .= "\n" . "<textarea name=' $field_name ' cols='40' rows='4'><? echo $" . " $field_name ; ?></textarea>" ;
}
else if( strtolower ( $field_type ) == "date" )
{
$fp .= "\n" . "<input type= 'text' name = ' $field_name ' size = ' $field_space +5' maxlength = '12' value = " . '"' . "<? echo $" . " $field_name ; ?>" . '"' . ">" ;
}
$fp .= "\n" . "</span></div></TD>" ;
$fp .= "\n" . "</TR>" ;
}
}
$fp .= "\n" . "<TR>" ;
$fp .= "\n" . "<TD class='darkrow3'> <div align='left'><span class='postdetails'>" ;
$fp .= "\n<B>" . ' Submit' . "</B>" ;
$fp .= "\n" . "</span></div></TD>" ;
$fp .= "\n" . "<TD class='darkrow3'> <div align='left'><span class='postdetails'><input type = 'submit' value = 'submit data'></span></div></TD>" ;
$fp .= "\n" . "</TABLE></FORM>" ;
//Field Generation end
//Form Handler (PHP) Generation
$field_definition = $this -> furnish ( $field_definition );
$data_definition = $this -> furnish ( $data_definition );
$fpp .= "<?php" ;
$fpp .= "\n //include_once('dbinfo.php');" ;
$fpp .= "\n" . '$dbuser = ' . "'" . $this -> db_user . "';" ;
$fpp .= "\n" . '$dbhost = ' . "'" . $this -> db_host . "';" ;
$fpp .= "\n" . '$dbpwd = ' . "'" . $this -> db_pwd . "';" ;
$fpp .= "\n" . '$dbname = ' . "'" . $this -> db_name . "';" ;
$fpp .= "\n" . "$" . "suppress = " . "$" . "HTTP_GET_VARS['suppress'];" ;
$fpp .= $handler ;
$fpp .= "\nif (!empty($" . $this -> key_field . ")){" ;
$query1 = '$query = "' . 'INSERT INTO ' . $this -> table_name . ' (' . $field_definition . ') VALUES ( ' . $data_definition . ' )";' ;
$query2 = '$result = mysql_query($query, $dblink);' ;
$fpp .= "\n" . '$dblink = mysql_connect($dbhost,$dbuser,$dbpwd) or die("Cannot Connect To Database");' ;
$fpp .= "\n" . 'mysql_select_db($dbname);' ;
$fpp .= "\n $query1 " ;
$fpp .= "\n $query2 " ;
$fpp .= "\n echo ('<p align = \"left\">Record Successfully Inserted</p>');" ;
$fpp .= "\n}" ;
$fpp .= "\n else if(empty($" . " $this -> key_field )" . ' && (count($HTTP_POST_VARS)>0)' . ") " ;
$fpp .= "\n echo ('<p align = \"left\">Record Contains Error, Please Check Again</p>');" ;
$fpp .= "\n?>" ;
$fpp .= "\n $fp " ;
$php_file = fopen ( " $this -> table_name " . "_insert.php" , "w" );
fwrite ( $php_file , $fpp );
fclose ( $php_file );
//End Handleer Generation
}
function getTypes ( $data )
{
$fld = explode ( "(" , $data );
return trim ( $fld [ 0 ]);
}
function getSize ( $data )
{
$fld = explode ( "(" , $data );
return trim ( str_replace ( ")" , "" , $fld [ 1 ]));
}
function readStyle ()
{
$file = fopen ( "style.txt" , "r" );
while (! feof ( $file ))
{
$data .= fgets ( $file , 1024 );
}
return $data ;
}
function furnish ( $data )
{
$data = trim ( $data );
$len = strlen ( $data );
$data = substr ( $data , 0 , $len - 1 );
return $data ;
}
function initialize ()
{
$this -> border = (empty( $this -> border ))? "0" : $this -> border ;
$this -> cellpad = (empty( $this -> cellpad ))? "1" : $this -> cellpad ;
$this -> cellspace = (empty( $this -> cellspace ))? "0" : $this -> cellspace ;
$this -> width = (empty( $this -> width ))? "100%" : $this -> width ;
$this -> modified_width = $this -> width ;
$this -> display_mode = (empty( $this -> display_mode ))? 0 : 1 ;
$this -> table_name = (empty( $this -> table_name ))? "" : trim ( $this -> table_name );
}
}
?>
Detailed Usage Example
<?php
include_once( "phpFormGen.php" );
$prg = new phpFormGenerator ();
//Decoration
$prg -> width = "100%" ;
$prg -> cellpad = "0" ;
$prg -> cellspace = "0" ;
$prg -> border = "0" ;
//Decoration End
mysql_connect ( "localhost" , "root" , "root" );
mysql_select_db ( "automation_system" );
//Which fields are available in input form?? this comes from
//following query
$res = mysql_query ( "select * from classtest" );
//Or you may say (select id, roll, name from classtest)
//Pass the resource to FormGenerator
$prg -> mysql_resource = $res ;
//login information for the handler PHP
$prg -> db_host = "localhost" ;
$prg -> db_name = "automation_system" ;
$prg -> db_pwd = "root" ;
$prg -> db_user = "root" ;
$prg -> table_name = "classtest" ;
//login and necessary info end
//This is the key field that user must type. If its empty, record
//will notbe inserted into table and an error message will be
//shown
$prg -> key_field = "id" ;
//Now generate The form file.
$prg -> generateInsert ();
?>
A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP , PHP Classes , Databases , MySQL , HTML and PHP Simple Mini Poll class library (SimPoll) Categories : PHP , PHP Classes , Databases , MySQL , Complete Programs Powerful php/mysql Pagination for up to 6 URL Params Categories : PHP , PHP Classes , Databases , MySQL , Navigation PHP and MySQL scripting for Muyltiple CheckBoxes Categories : HTML and PHP , MySQL , Databases , PHP MySQL Connection/Query Class Categories : Databases , MySQL , PHP , PHP Classes Simple usersOnline class - keep track of how many users are online on your site Categories : PHP , PHP Classes , Databases , MySQL PostGreSQL and MySQL 2 in 1 db Manager Categories : PHP , PHP Classes , Databases , PostgreSQL , MySQL phpMyDataGrid 2007 Categories : AJAX , PHP Classes , MySQL , Databases , HTML and PHP Pull Down Surfing - Surf on Change Categories : Java Script , MySQL , HTML and PHP , PHP , Databases MySQL Class to ease Database connectivity Categories : MySQL , PHP Classes , Databases , PHP dynamic table columns Categories : PHP , HTML and PHP , Arrays , Databases , MySQL Message of the Day - Random Message (Needs MySQL!) Categories : Databases , HTML and PHP , PHP , MySQL Functions for loading images into a MySQL database and displaying them. Categories : Graphics , HTML and PHP , MySQL , PHP , Databases Required form fields that pull from MySQL database Categories : PHP , HTML and PHP , Databases , MySQL [PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more. Categories : PHP , PHP Classes , Databases , MS SQL Server , MySQL
Jean-Michel Beuken wrote : 1278
Hello,
very interesting script...
I`m not yet studeid the script but do you think that is it difficult to add support of field types enum and float ?
thanks
regards
jmb
Tom McDonald wrote : 1643
Most excellent!