Location: PHPKode > scripts > Tablemaint > tablemaint/tablemaint.php
<?

/*
    * Example script demonstration use of TABLEMAINT class.
   *  This is my first class and it works fine.
   *
    * Marcelo Costales
    * hide@address.com
   * Each table must have 2 fields ( example: keycode and description ). 
   * 
   * This script is free and can be used in any program commercial or not.

 */


class tablemaint {


	// Conect to database
	function db_open ($db_hostname,$db_username,$db_password,$db_name) {   
	         $this->db = mysql_connect($db_hostname,$db_username,$db_password) or die ("Error");
	         mysql_select_db($db_name, $this->db) or die ("Error");
	}

	// Search a key code
	function search ($coddata,$desdata,$tabla,$campocod) {

 	        $this->settings($tabla,$campocod,$coddata); 

	         if (($this->row) and ($this->row = mysql_fetch_array($this->result))) { 
	                      return $this->row[$desdata];
		       } else {
		 return "Code does not  exists!";
        	          }
	}	

	// Add a code
	function add ($coddata,$desdata,$tabla,$campocod,$campodes) {

  	        $this->settings($tabla,$campocod,$coddata);   

 	        if ($this->row) {  
	                   return "Code already exists!"; 
		   } else {		
	                   $this->result = mysql_query("insert into $tabla ($campocod,$campodes) values ('$coddata','$desdata')") or die ("Error");    
	                  return "Record inserted!";
	         }
	}


	// Delete a code
	function delete ($coddata,$tabla,$campocod) {

   	         $this->settings($tabla,$campocod,$coddata);   

 	         if ($this->row) {  
	                    $this->result = mysql_query("delete from $tabla where $campocod = '$coddata'") or die ("Error");    
		      return "Record deleted!";
		           } else {
		       return "Code does not exist!";  		
	         }
	}


	// Modify data
	function modify ($coddata,$desdata,$tabla,$campocod,$campodes) {

  	         $this->settings($tabla,$campocod,$coddata);

   	          if ($this->row) {  
	                    $this->result = mysql_query("update $tabla set $campodes='$desdata' where $campocod='$coddata'") or die ("Error");    
		      return "Record updated!";
		            } else {
		       return "Code does not exist!"; 	
	           }
	}


	// Settings
	function settings ($tabla,$campocod,$coddata) {
 	        $this->tabla    = $tabla;
	        $this->campocod = $campocod;
	        $this->coddata  = $coddata;
	        $this->verify();	
	}


	// Check data
	function verify() {
 	        $this->result = mysql_query("SELECT * FROM $this->tabla where $this->campocod = '$this->coddata'") or die ("Error ");	
	        $this->row = mysql_num_rows($this->result);   
	}

}  // End of class: tablemaint




/*

Database : any name. 

Create table 'data' for this demo. You can use your own name.

CREATE TABLE data (
  codigo varchar(10) NOT NULL default '',
  descrip varchar(20) NOT NULL default ''
) TYPE=MyISAM;

INSERT INTO data VALUES ('10', 'Article 10');
INSERT INTO data VALUES ('20', 'Article 20');
INSERT INTO data VALUES ('30', 'Article 30');

*/


// How to use it 

$tblmaint = new tablemaint;
$tblmaint->db_open("localhost","root","","database_name");

// Add:   add(keycode to add , description , table name , field1 to search and add ,  field2 to add)
echo $tblmaint->add("40","Article number 40","data","codigo","descrip");

// Delete:  delete(keycode to delete , table name , field1 to search)
echo $tblmaint->delete("40","data","codigo");

// Modify:  modify(keycode to search , description to update , table name , field1 to search ,  field2 to update)
echo $tblmaint->modify("40","Article 40 updated","data","codigo","descrip");

// Search:  search(keycode to search , field2 to return, table name , field1 to search)
echo $tblmaint->search("20","descrip","data","codigo");

?>
Return current item: Tablemaint