This class will generate a new class base on a database table.
The new class will be easy to add and
update the record of that table.
private $tablename;
This name of the table we want to
generate new class.
private $fields;
This array of the fields’ names of the
table...
public function genpo();
Generate new class
code in string.
1) Input table name and fields’ name array to initialize the genpo class.
2) Call the function genpo to generate the new classs.
Example:
$dbserver="localhost";$dbuser="annie";$dbpass="annie";
$link=mysql_connect($dbserver,$dbuser, $dbpass);
$dbname="annie";$tablename="compinfo";
//Get the array of the fileds’ names of the table.
$fields = mysql_list_fields ( $dbname , $tablename , $link );
$columns = mysql_num_fields ( $fields );
$fieldnames = array();
for ( $i = 0 ; $i < $columns ; $i ++) {
$fieldnames[]= mysql_field_name ( $fields , $i ) ;
}
$g = new genpo($tablename,$fieldnames);
echo "<textarea rows=100 cols=100>";
echo $g->genpo() ;
echo "</textarea>";
1) Set every filed
2) Get every filed
3) public function db_save();
If the class entiry object is exist db_save will cause update otherwise it will insert a new record.
4) You can check the class class.compinfo.php and the example.UseNewClass.php which use the class.compinfo.php to know more .
1) include the need php pages
2) initialize how to connect the database
3)
Use the class to oprate the
table which we create the class.
<?php
include "ado/adodb.inc.php";
include "ado/class.ConnDb.php";
include "class.compinfo.php"; //this is the class witch is generated by us.
//initialize all parameter
$dbserver="localhost";$dbuser="annie";$dbpass="annie";
$link=mysql_connect($dbserver,$dbuser, $dbpass);
$dbname="annie";
#The location and credentials for the slave database
#server used by this server
$dbSlaveCredentials['server'] = $dbserver;
$dbSlaveCredentials['user'] = $dbuser;
$dbSlaveCredentials['password'] = $dbpass;
#The location and credentials for the master database
#server (usually on localhost) used by this server
$dbMasterCredentials['server'] = $dbserver;
$dbMasterCredentials['user'] = $dbuser;
$dbMasterCredentials['password'] = $dbpass;
$applicationDb = new ConnDb($dbname); //this global value will be used by generated class.
//use the class to oprater table compinfo.
$cinfoin= new compinfo();
$cinfoin->set_compname("a comp name");
$cinfoin->set_mylink("a comp link");
$cinfoin->set_intime("2008-09-17");
$cinfoin->db_save();
$id=$cinfoin->get_id();
echo "id=".$id;
$cinfoout = new compinfo($id);
echo "<br>";
echo $cinfoout->get_compname()."<br>";
echo $cinfoout->get_mylink()."<br>";
?>