dbForm PHP Project
Basic Demonstration Source Code

This code is a basic example of what dbForm is capable of. It is a simple example but has enough to show basically all of the field types available. This example could have been simpler but it would not have shown all of the field options.

There is really only six parts to the source code

  1. Includes and setup
    This section includes the necessary class files and creates the ADODB database connection.
  2. Create the dbForm instance
    This creates the instance of the dbForm class. The only parameter which is manditory is the first one, and there are others not used in this example (See manual for more details)
  3. Pre-Processing Setup
    This section is where you make any alterations to the default table before processing. Any methods that effect the design of the form are called here, including adding and removing fields and buttons, changing field types etc.
  4. Processing Form
    This processes any data passed from the form and performs any actions.
  5. Pre-Display Setup
    During this part we retrieve any select list data and set the template to be used.
    Note: Although searchid is a selection list we do not have to retrieve the data for this list as it is done automatically for us.
  6. Display Form
    This displays the form using the given template. At this point we are finished although it is good to cleanup database connections etc.

demo1.php

<?php
/*
 * Basic Demonstration
 *
 * Example of using dbForm to display
 * and process a web-form for a database table.
 *
 * Uses a person table with the following MySQL structure
 * CREATE TABLE `person` (
 *   `personid` int(11) NOT NULL auto_increment,
 *   `firstname` varchar(50) default NULL,
 *   `lastname` varchar(50) default NULL,
 *   `username` varchar(20) default NULL,
 *   `password` varchar(32) default NULL,
 *   `notes` text,
 *   `accessid` int(11) default '1',
 *   `active` tinyint(1) unsigned default '1',
 *   `canceldate` date default NULL,
 *   PRIMARY KEY  (`personid`),
 *   UNIQUE KEY `personid` (`personid`),
 *   KEY `personid_2` (`personid`)
 * ) TYPE=MyISAM COMMENT='person table demo';
 *
 * and the access table with the following MySQL structure and data
 * CREATE TABLE `access` ( 
 *   `accessid` tinyint(3) NOT NULL auto_increment, 
 *   `name` varchar(20) default NULL, 
 *   PRIMARY KEY (`accessid`), 
 *   UNIQUE KEY `accessid` (`accessid`), 
 *   KEY `accessid_2` (`accessid`) 
 * ) TYPE=MyISAM COMMENT='demo access table'; 
 *
 * INSERT INTO access VALUES("1", "Admin"); 
 * INSERT INTO access VALUES("2", "User"); 
 * 
 */

///////////////////////////////////
// Section 1.                    //
//    Includes and setup         //
///////////////////////////////////

// include the necessary files
include_once("/path/to/adodb/adodb.inc.php");
include_once(
"/path/to/dbform/formFields.inc");
include_once(
"/path/to/dbform/template.inc");
include_once(
"/path/to/dbform/dbForm.inc");

// create a database connection
$conn = &ADONewConnection('mysql');
$conn->Connect('localhost''username''password''datasource');
$conn->debug false;
$ADODB_FETCH_MODE ADODB_FETCH_ASSOC;


///////////////////////////////////
// Section 2.                    //
//    Create the dbForm instance //
///////////////////////////////////

// create the form for the person table
$dbForm = new dbForm("Person"$conn"person");


///////////////////////////////////
// Section 3.                    //
//    Pre-Processing Setup       //
///////////////////////////////////

// MD5 hash passwords before storing in the database
// requires the mhash extension to be loaded
$dbForm->_hashPasswords=true;

// set the query for the search field with ", " glueing fields together
// and a default item of "Select a Person"
$dbForm->setSearchField("personid","select personid, lastname, firstname from person order by lastname, firstname"", ""Select a Person");

// change the password field to a password type
$dbForm->changeFieldType("password","password");

// make accessid a selection list
$dbForm->changeFieldType("accessid","select");

// make active a checkbox with 1 as the checked value,
// and 0 as the unchecked value
$dbForm->setCheckbox("active",1,0);

// set the Date min and max
$dbMinYear 1950;
$dbMaxYear 2050;

///////////////////////////////////
// Section 4.                    //
//    Processing Form            //
///////////////////////////////////

// process the data passed to/from the form
$dbForm->processForm();


///////////////////////////////////
// Section 5.                    //
//    Pre-Display Setup          //
///////////////////////////////////

// get the values for the accessid selection list
// note no default value has been set
$dbForm->getSelectValues("accessid","select accessid, name from access");

// set the template file being used
$dbForm->setTemplateFile("demo1.tpl");


///////////////////////////////////
// Section 6.                    //
//    Display Form               //
///////////////////////////////////

// display the form
$dbForm->displayForm();
?>

Template File

While it might not appear it at first glance this is a fairly simple template. The main thing of note is the use of the {:action="..."}...{:end action} tags. These tags are used to decide when parts of the form, in this case field and buttons, will be shown.

The {:action="..."} tag contains a comma seperated list of actions. Note that 'null' is a special action that exists when the form is first loaded.

demo1.tpl

{:form}
<table border="1" width="500" align="center">
   <caption>{:FORMNAME}</caption>
{:action="null,select,insert,update,delete,cancel"}
   <tr>
      <td width="100">Search:</td>
      <td width="300">{:FIELD="dbform_searchid"}</td>
      <td width="100">{:button="select"}</td>
   </tr>
   <tr>
      <td colspan="3">&nbsp;{:status}</td>
   </tr>
{:end action}
   <tr>
      <td colspan="3" align="center">
{:action="null,select,insert,update,delete,cancel"}
         {:BUTTON="new"}
{:end action}
{:ACTION="select,insert,update"}
         {:BUTTON="update"}
         {:button="delete"}
{:END ACTION}
{:ACTION="new"}
         {:button="insert"}
         {:button="cancel"}
{:END ACTION}
      </td>
   </tr>
{:ACTION="new,insert,select,update"}
{:EACH FIELD}
   <tr>
      <td width="100">{:fieldname}</td>
      <td width="400" colspan="2">{:field}</td>
   </tr>
{:END FIELD}
{:end action}
</table>
{:hidden}
</form>

Download Demo files

The files that have been used in this demonstration can be downloaded so you can try them out for yourself.

demo1.zip