Location: PHPKode > scripts > MySQL Form Generator > mysql-form-generator/class.forms.php
<?
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
#                                                         #
#           Form Class - The MySQL Form Generator         #
#           Tobie van der Spuy - 2001                     #
#           hide@address.com                          #
#                                                         #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

class Form {
	  
	  var $name;  // Form Variable Name
	  var $title;  // Form Title
	  var $css;  // Form CSS File
	  var $action;  // Form Action
	  var $method;  // Form Method
	  var $enctype;  // Form Enctype
	  var $submit;  // Form Submit
	  var $tables;  // array of table classes to be used
	  var $fields;  // array of final form fields
	  var $ids;  // array of final form field ID's
	  var $details;  // array of final field details
	  var $equal;  // array of values that use the same form input but have different names
	  var $temp; // TEMP HTML
	  var $form; // FINAL HTML
	  var $result;  // FINAL HTML RESULT
	  

	  // Form Constructor Function <
	  // ---------------------------
	  function Form ($name,$title,$action,$method,$enctype,$submit,$css) {

	  		   if ($action == "") { $method = $PHP_SELF; }
	  		   if ($method == "") { $method = "POST"; }
	  		   if ($enctype == "") { $enctype = "text"; }
	  		   if ($submit == "") { $submit = "submit"; }
			   $this->name = $name;
			   $this->title = $title;
	  		   $this->css = $css;
	  		   $this->action = $action;
	  		   $this->method = $method;
	  		   $this->enctype = $enctype;
	  		   $this->submit = $submit;
			   $this->tables = array();
			   $this->fields = array();
			   $this->ids = array();
	  		   $this->details = array();
	  		   $this->equal = array();
	  		   $this->temp = "";
	  		   $this->form = "";
	  		   $this->result = "";
			   }


	  // Add a new table to the form <
	  // -----------------------------
	  function addtable ($db,$name,$ignore,$passwd,$hide,$action,$where,$order,$limit) {

	  		   array_push($this->tables,$name);
			   $GLOBALS[$name] = new MySQLTable($db,$name,$ignore,$passwd,$hide,$action,$where,$order,$limit);
			   }


	  // Add custom descriptions to the form fields <
	  // --------------------------------------------
	  function describe ($details) {

	  		   $this->details = explode("/:/",$details);
			   }


	  // Add custom descriptions to the form fields <
	  // --------------------------------------------
	  function equal ($equal) {

	  		   $this->equal = $equal;
			   }


	  // Build an array of form fields <
	  // -------------------------------
	  function makefields () {

			   for ($i = 0; $i < sizeof($this->tables); $i++) {
			   	   $table = $this->tables[$i];
				   $temp = $GLOBALS[$table]->fields;
				   $final = array();
				   $ids = array();
				   for ($j = 0; $j < sizeof($temp); $j++) {
					   $fieldn = $temp[$j];
					   $name = $GLOBALS[$fieldn]->varname;
					   $id = $GLOBALS[$fieldn]->ID;
					   $ieq = array_flip($this->equal);
					   if (in_array($name,$this->equal)) { $name = $ieq[$name]; }
					   array_push($ids,$id);
					   array_push($final,$name);
					   }
				   $merged1 = array_merge($final,$this->fields); 
				   $merged2 = array_merge($ids,$this->ids); 
				   $this->fields = $merged1;
				   $this->ids = $merged2;
				   }
			   $this->fields = array_unique($merged1);
			   $this->ids = array_unique($merged2);
			   }

			   
	  // Build the final form html code <
	  // --------------------------------
	  function build ($c1width,$c2width,$c3width,$rows,$cols) {
	  		   
			   $width = $c1width + $c2width + $c3width;
			   
			   $this->form .= "<center><table class=\"formtable\" width=\"$width\">\n<tr>\n";
			   $this->form .= "<th align=center colspan=3><h3>" . $this->title . "</h3>\n";
			   $this->form .= "<form action=\"$this->action\" method=\"$this->method\" ecntype=\"$this->enctype\" onsubmit=\"return true\">\n";
			   $this->form .= "<SCRIPT defer language=Javascript type=text/javascript>\n";
			   $this->form .= "<!--\nform_submitted=false\n//-->\n</SCRIPT>";
			   $this->form .= "<NOSCRIPT><!-- You don't have Javascript support enabled! --></NOSCRIPT>\n";
			   $this->form .= "</th>\n</tr>\n";
			   
			   $total = count($this->fields);
	  		   for ($i = 0; $i < $total; $i++) {
			   	   $temp = $this->fields[$i];
			   	   if ($temp == "") { $total++; }
				   else {
				   		//if (in_array($temp,$this->select)) { }
						$tmpid = $this->ids[$i];
						if ($this->details[$i] == "") { $detailz = $tmpid . " - " . $temp; }
						else { $detailz = $this->details[$i]; }
				   		if ($GLOBALS[$tmpid]->hidden == "0") {
				   	  	   $this->form .= $GLOBALS[$tmpid]->build($detailz,$c1width,$c2width,$c3width,$rows,$cols);
					  	   }
						else {
							 $this->temp .= $GLOBALS[$tmpid]->build($detailz,$c1width,$c2width,$c3width,$rows,$cols);
							 }
				   		}
				   }

			   $this->form .= "<tr>\n<td width=\"$c1width\" align=right></td><td width=\"$c2width\"></td>" . $this->temp . "<td width=\"$c3width\"></td>\n</tr>\n";
			   $this->form .= "<tr>\n<td width=\"$c1width\" align=right><input type=reset value=\"Reset\"></input></td><td width=\"$c2width\"></td>";
			   $this->form .= "<td width=\"$c3width\"><input class= \"formsubmit\" type=submit name=\"$this->submit\" ";
			   $this->form .= "id=submit_button onclick=\"if(this.disabled || typeof(this.disabled)=='boolean') this.disabled=true ; form_submitted_test=form_submitted ; form_submitted=true ; form_submitted=(!form_submitted_test || confirm('You have already submitted this form, submitting it again will create a duplicate! Continue?')) ; if(this.disabled || typeof(this.disabled)=='boolean') this.disabled=false ; sub_form='' ; return true\" ";
			   $this->form .= "value=\"Submit\"></input></td>\n</tr>\n";
			   $this->form .= "<tr><td colspan=3></form></td></tr></table></center>\n";
			   
			   return $this->form;
			   }


	  // Submit the form fields <
	  // ------------------------
	  function submit () {

			   $GLOBALS[error] = "";
			   for ($j = 0; $j < sizeof($this->tables); $j++) {
			   	   $table = $this->tables[$j];
				   if ($GLOBALS[$table]->submit($this->name)) { }
				   else { $GLOBALS[error] .= "Error in $table<br>\n"; }
				   }
			   if ($GLOBALS[error] == "") {
			   	  return True;
				  }
			   else { 
			   		return False;
					}
			   }

			   
	  // Build the final form html code <
	  // --------------------------------
	  function Result ($c1width,$c2width,$c3width) {
	  		   
			   $width = $c1width + $c2width + $c3width;
			   
			   $this->result .= "<center><table class=\"formtable\" width=\"$width\">\n<tr>\n";
			   $this->result .= "<th align=center colspan=3><h3>" . $this->title . " - Results</h3></th>\n</tr>\n";
			   
			   $total = count($this->fields);
	  		   for ($i = 0; $i < $total; $i++) {
			   	   $temp = $this->fields[$i];
			   	   if ($temp == "") { $total++; }
				   else {
				   		//if (in_array($temp,$this->select)) { }
						$tmpid = $this->ids[$i];
						if ($this->details[$i] == "") { $detailz = $tmpid . " - " . $temp; }
						else { $detailz = $this->details[$i]; }
						$this->result .= $GLOBALS[$tmpid]->Result($detailz,$c1width,$c2width,$c3width);
				   		}
				   }

			   $this->result .= "<tr><td colspan=3></td></tr>\n";
			   $this->result .= "<tr><td align=center colspan=3><i>Updated Succesfully!</i></td></tr></table></center>\n";
			   
			   return $this->result;
			   }
	  }
?>
Return current item: MySQL Form Generator