WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : phpFormGenerator for Dynamic Form Generation from MySQL
Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP Click here to Update Your Picture
Hasin Hayder
Date : Jan 20th 2005
Grade : 3 of 5 (graded 15 times)
Viewed : 13714
File : 4061.zip
Images : No Images for this code example.
Search : More code by Hasin Hayder
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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>&nbsp;".$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>".'&nbsp;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
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
Alternating background color for HTML table rows
Categories : PHP, Databases, MySQL, HTML and PHP
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Automatically printing the contents of an sql table in MySQL.
Categories : MySQL, PHP, HTML and PHP, Databases
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
Pull Down Surfing - Surf on Change
Categories : Java Script, MySQL, HTML and PHP, PHP, Databases
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
Dynamically generated pop-ups (Select items)
Categories : PHP, HTML and PHP, MySQL, Databases
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
Creating thumbnails from MySQL Blobs online
Categories : PHP, MySQL, Graphics, HTML and PHP, Databases
Record Set Paging with PHP (RSP)
Categories : PHP, MySQL, Navigation, Databases, HTML and PHP
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
 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!