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

class MySQLField {
	  
	  var $ID;  // Field ID
	  var $name;  // Name of field 
	  var $type;  // Field type
	  var $null;  // Null or Not Null 
	  var $key;  // Field index or key value in table 
	  var $def;  // Default field value 
	  var $extra;  // Extra field info 
	  var $len;  // Length of field 
	  var $passwd;  // Is this field a password field?
	  var $value;  // Value from DB, and later new value to be put into DB
	  var $varname;  // Varname of field, used to find value after submit 
	  var $hidden;  // IF hidden, used to generate as hidden
	  var $formtype;  // What type of forminput this object will generate 
	  

	  function MySQLField ($ID,$name,$type,$null,$key,$def,$extra,$len,$passwd,$hidden,$value) {

			   $this->ID = $ID;
			   $this->name = $name;
			   $this->type = $type;
			   $this->null = $null;
			   $this->key = $key;
			   $this->def = $def;
			   $this->extra = $extra;
			   $this->len = $len;
			   $this->passwd = $passwd;
			   $this->value = $value;
	  		   $this->varname = $name;
	  		   $this->hidden = $hidden;
	  		   $this->formtype = "";
			   }

	  
	  function SetValue ($value) {

			   $this->value = $value;
			   }


	  function SetVar ($varname) {

			   $this->varname = $varname;
			   }


	  function Build ($details,$c1width,$c2width,$c3width,$rows,$cols) {

			   $this->value = stripslashes($this->value);
			   
			   if ($len >= 20) { $maxsize = "20"; }
			   else { $maxsize = $len; }
			   
			   if ($this->passwd == "1") { $type = "password"; }
			   elseif ($this->hidden == "1") { $type = "hidden"; }
			   else { $type = "text"; }

			   if ($select != "") {
			   	  $array1 = $select;
				  $keys = array_keys($array1);
				  $output = "";
				  $output .= "<tr>\n<td align=right width=\"$c1width\" class=\"formdetails\">$details</td><td width=\"$c2width\"></td><td align=left width=\"$c3width\">\n";
				  $output .= "<select name=\"" . $this->name . "\" class=\"forminput\">\n";
				  for ($i = 0; $i < sizeof($array1); $i++) {
				  	  $valz = $keys[$i];
					  if ($array1[$valz] == $this->value) { $selected = " selected"; }
					  else { $selected = ""; }
					  	   $output .= "<option value=\"$valz\"$selected>$array1[$valz]</option>\n";
					  	   }
					  $output .= "</select></td>\n</tr>\n";
				  	  }
			   else {
			   		
					if ($this->hidden == "1") {
				 	   $output .= "<input type=\"$type\" name=\"" . $this->name . "\" value=\"" . $this->value . "\" size=\"$maxsize\" maxlength=\"$len\" class=\"forminput\">";
				 	   $output .= "</input>\n";
					   }
					else {
			   
			   		     // Check if type is Integer, Real, Date/Time, Char/Varchar or Year
		 	  		     // ---------------------------------------------------------------
		 	  		     if ((eregi("int",$this->type)) || (eregi("double",$this->type)) || (eregi("float",$this->type)) || (ereg("decimal",$this->type)) || (ereg("date",$this->type)) || (ereg("time",$this->type)) || (ereg("year",$this->type)) || (ereg("char",$this->type))) {
		 	  	 	        $output = "<tr>\n<td align=right width=\"$c1width\" class=\"formdetails\">$details</td><td width=\"$c2width\"></td><td align=left width=\"$c3width\" class=\"formvalue\">";
				 	        $output .= "<input type=\"$type\" name=\"" . $this->name . "\" value=\"" . $this->value . "\" size=\"$maxsize\" maxlength=\"$len\" class=\"forminput\">";
				 	        $output .= "</input></td>\n</tr>\n";
				 	        }

					     // Check if type is and if len = 1, make checkbox, else make combobox
					     // ------------------------------------------------------------------
					     elseif (eregi("enum",$this->type)) {
						 		$selvalue = str_replace("enum(","",$this->type);
								$selvalue = str_replace("'","",$selvalue);
								$selvalue = str_replace("(","",$selvalue);
								$selvalue = str_replace(")","",$selvalue);
								$selvalue = explode(",",$selvalue);
								$output = "<tr>\n<td align=right width=\"$c1width\" class=\"formdetails\">$details</td><td width=\"$c2width\"></td><td align=left width=\"$c3width\" class=\"formvalue\">";
								$output .= "<select name=\"" . $this->name . "\" class=\"formcombobox\">";
								for ($i = 0; $i < sizeof($selvalue); $i++) {
									if ($this->value == $selvalue[$i]) { $selected = " selected"; }
									else { $selected = ""; }
									$output .= "<option value=\"$selvalue[$i]\"$selected>$selvalue[$i]</option>";
									}
								$output .= "</select></td>\n</tr>\n";
			  		            } 
			             // Check if type set and make combo box
			             // ------------------------------------
			             elseif (eregi("set",$this->type)) {
			  		            $selvalue = str_replace("set","",$this->type);
					            $selvalue = str_replace("'","",$selvalue);
					            $selvalue = str_replace("(","",$selvalue);
					            $selvalue = str_replace(")","",$selvalue);
					            $selvalue = explode(",",$selvalue);
					            $output = "<tr>\n<td align=right width=\"$c1width\" class=\"formdetails\">$details</td><td width=\"$c2width\"></td><td align=left width=\"$c3width\" class=\"formvalue\">";
					            $output .= "<select name=\"" . $this->name . "\" class=\"formcombobox\">";
					            for ($i = 0; $i < sizeof($selvalue); $i++) {
					 	            if ($selvalue[$i] == $this->value) { $selected = " selected"; }
									else { $selected = ""; }
						            $output .= "<option value=\"$selvalue[$i]\"$selected>$selvalue[$i]</option>";
						            }
					            $output .= "</select></td>\n</tr>\n";
					            }
			             // Check if type is Text and make textbox
			             // --------------------------------------
			             elseif (eregi("text",$this->type)) {
			  		            $output = "<tr><td align=center colspan=3 class=\"formdetails\">$details</td></tr>\n<tr><td align=center colspan=3 class=\"formvalue\">";
					            $output .= "<textarea name=\"" . $this->name . "\" class=\"formtextbox\" cols=\"$cols\" rows=\"$rows\">" . $this->value . "</textarea></tr>\n";
					            } 
			             // Check if type is Blob, Set or Enum and set output to ""
			             // -------------------------------------------------------
			             elseif (eregi("blob",$this->type)) {
			  		            $output = "";
					            } 
			             // If anything else, set output to ""
			             // ----------------------------------
			             else {
			  	              $output = "";
				              } 
		 	             }
					}

		       // Return Final String
		       // -------------------
		       return $output;
			   }
			   

	  function GetValue () {

			   $var = $this->varname;
			   $temp = $GLOBALS[HTTP_POST_VARS];
			   $this->value = $temp[$this->varname];
			   $this->value = addslashes($this->value);
			   return true;
			   }
			   

	  function Result ($details,$c1width,$c2width,$c3width) {

			   $value = stripslashes($this->value);
			   $value = nl2br($value);
			   
			   if ($this->passwd != "1" || $this->hidden != "1") { 
			   	  // Check if type is Integer, Real, Date/Time, Char/Varchar or Year
				  // ---------------------------------------------------------------
				  if ((eregi("int",$this->type)) || (eregi("double",$this->type)) || (eregi("float",$this->type)) || (ereg("decimal",$this->type)) || (ereg("date",$this->type)) || (ereg("time",$this->type)) || (ereg("year",$this->type)) || (ereg("char",$this->type)) || (eregi("enum",$this->type)) || (eregi("set",$this->type))) {
				  	 $output = "<tr>\n<td align=right width=\"$c1width\" class=\"formdetails\"><b>$details</b></td><td width=\"$c2width\"></td>";
					 $output .= "<td align=left width=\"$c3width\" class=\"formvalue\">" . $value . "</td>\n</tr>\n";
					 }

				  // Check if type is Text and make textbox
				  // --------------------------------------
				  elseif (eregi("text",$this->type)) {
				  		 $output = "<tr><td align=right width=\"$c1width\" class=\"formdetails\"><b>$details</b></td><td width=\"$c2width\"></td><td width=\"$c3width\"></td></tr>\n";
						 $output .= "<tr><td align=center colspan=3 class=\"formvalue\">" . $value . "</tr>\n";
						 $output .= "<tr><td align=center colspan=3 class=\"formvalue\"></tr>\n";
						 } 

				  // Check if type is Blob, Set or Enum and set output to ""
				  // -------------------------------------------------------
				  elseif (eregi("blob",$this->type)) {
				  		 $output = "";
						 } 

				  // If anything else, set output to ""
				  // ----------------------------------
				  else {
				  	   $output = "";
					   } 
				  }
			   else { $output = ""; }
			   		

		       // Return Final String
		       // -------------------
		       return $output;
			   }
	  }
?>

Return current item: MySQL Form Generator