Location: PHPKode > projects > JACR - Just Another Checkbook Register > jacr_0.0.4/jacr-platform/DBConn/table.class.php
<?php 
include('DBConn.inc.php');
class DBTable
{
  var $tablename;         // table name
  var $dbname;            // database name
  var $rows_per_page;     // used in pagination
  var $pageno;            // current page number
  var $lastpage;          // highest page number
  var $fieldlist;         // list of fields in this table
  var $data_array;        // data from the database
  var $errors;            // array of error messages
  var $pKEY;
  var $uniqueKEY;

#Class Constructor

#This is immediately followed by the constructor method. Note that each derived class should have its own constructor containing proper values:

  public function __construct ()
  {
    $this->tablename       = 'default';
	global $dbname;
    $this->dbname          = $dbname;
    $this->rows_per_page   = 10;
    
    $this->fieldlist = array(
					    'column1' => array('int(11)', 'NOT NULL', 'None', 'auto_increment')
					    );
    $this->pKEY = 'colum1';
  }
	public function install()
	{
		$query = "CREATE TABLE IF NOT EXISTS `$this->tablename` (";
		foreach ($this->fieldlist as $field => $params) {
			$query .= " `$field` $params[0] $params[1] ";
			if ($params[2] != NULL)
				$query .= " default $params[2] ";
			$query .= $params[3] . ",";
		}
		if( isset($this->pKEY) )
			$query .= " PRIMARY KEY (`$this->pKEY`),";
		if( isset($this->uniqueKEY) )
			foreach($this->uniqueKEY as $u)
			$query .= " UNIQUE KEY `$u` (`$u`),";
		$query = rtrim($query, ",");
		$query .= ')';
		#echo $query;
		$this->execute($query);
			
	}
public function getVar($key)
{
	if ( isset($_POST[$key]) )
		return $_POST[$key];
	elseif ( isset($_COOKIE[$key]) )
		return $_COOKIE[$key];
	else
		return NULL;
}
   public function getData ($where, $orderBy)
   {
      $this->data_array = array();
      $pageno          = $this->pageno;
      $rows_per_page   = $this->rows_per_page;
      $this->numrows   = 0;
      $this->lastpage  = 0;

      global $dbconnect, $query;
      $dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);

      if (empty($orderBy)) {
         $order_str = NULL;
      } else {
         $order_str = "ORDER BY $orderBy";
      } // if
      if (empty($where)) {
         $where_str = NULL;
      } else {
         $where_str = "WHERE $where";
      } // if

      $query = "SELECT count(*) FROM $this->tablename $where_str";
      $result = mysql_query($query, $dbconnect) or trigger_error("SQL", E_USER_ERROR);
      $query_data = mysql_fetch_row($result);
      $this->numrows = $query_data[0];


      if ($this->numrows <= 0) {
         $this->pageno = 0;
         return;
      } // if

      if ($rows_per_page > 0) {
         $this->lastpage = ceil($this->numrows/$rows_per_page);
      } else {
         $this->lastpage = 1;
      } // if


      if ($pageno == '' OR $pageno <= '1') {
         $pageno = 1;
      } elseif ($pageno > $this->lastpage) {
         $pageno = $this->lastpage;
      } // if
      $this->pageno = $pageno;


      if ($rows_per_page > 0) {
         $limit_str = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
      } else {
         $limit_str = NULL;
      } // if


      $query = "SELECT * FROM $this->tablename $where_str $order_str $limit_str";
      $result = mysql_query($query, $dbconnect) or trigger_error("SQL", E_USER_ERROR);
	 #echo $query;
      while ($row = mysql_fetch_assoc($result)) {
         $this->data_array[] = $row;
      } // while


      mysql_free_result($result);
   
      return $this->data_array;
      
   } // getData

	public function getRecord($pKEYvalue)
	{
		$record = $this->getData( $this->pKEY."=".$pKEYvalue );
	 	return ( $record[0] );
	}
   public function insertRecord ($fieldarray)
   {
	   #printArray($fieldarray);
      $this->errors = array();

		#Connecnt to db
      global $dbconnect, $query;
      $dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);



      $fieldlist = $this->fieldlist;
      foreach ($fieldarray as $field => $fieldvalue) {
		  #echo "$field => $fieldvalue <br/>";
         if (!array_key_exists($field, $fieldlist)) {
            unset ($fieldarray[$field]);
         } // if
      } // foreach


      $query = "INSERT INTO $this->tablename SET ";
      foreach ($fieldarray as $item => $value) {
		  #echo "$item => $value <br/>";
         $query .= "$item='$value', ";
      } // foreach


      $query = rtrim($query, ', ');

	 #echo $query;
      $result = @mysql_query($query, $dbconnect);
      if (mysql_errno() <> 0) {
         if (mysql_errno() == 1062) {
            $this->errors[] = "A record already exists with this ID.";
         } else {
            trigger_error("SQL", E_USER_ERROR);
         } // if
      } // if

      return;
   	   
   } 


   public function updateRecord ($fieldarray)
   {
	 
      $this->errors = array();


      global $dbconnect, $query;
      $dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);

      $fieldlist = $this->fieldlist;
      foreach ($fieldarray as $field => $fieldvalue) {
		 #echo" $field => $fieldvalue <br/>";
         if (!array_key_exists ($field, $fieldlist)) {
            unset ($fieldarray[$field]);
         } // if
      } // foreach

      $where  = NULL;
      $update = NULL;
      foreach ($fieldarray as $item => $value) {
		 #echo" $item => $value <br/>";
         if ( $item == $this->pKEY ) {
            $where .= "$item='$value' AND ";
         } else {
            $update .= "$item='$value', ";
         } // if
      } // foreach


      $where  = rtrim($where, ' AND ');
      $update = rtrim($update, ', ');


      $query = "UPDATE $this->tablename SET $update WHERE $where";
	  #echo "<h1> $query </h1>";
      $result = mysql_query($query, $dbconnect) or trigger_error("SQL", E_USER_ERROR);
      
      return;
      
   } // updateRecord


   public function deleteRecord ($fieldarray)
   {
      $this->errors = array();


      global $dbconnect, $query;
      $dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);


      $fieldlist = $this->fieldlist;
      $where  = NULL;
      foreach ($fieldarray as $item => $value) {
		 #echo " $item => $value <br/>";
         if ( $item == $this->pKEY ) {
            $where .= "$item='$value' ";
         } // if
      } // foreach


      $query = "DELETE FROM $this->tablename WHERE $where";
      $result = mysql_query($query, $dbconnect) or trigger_error("SQL", E_USER_ERROR);
      
      return;
      
   } // deleteRecord
   public function execute($sql)
   {
      global $dbconnect, $query;
      $dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);
      $result = mysql_query($sql, $dbconnect) or trigger_error("SQL", E_USER_ERROR);
	 return($result);
   }
} // end class
?>
Return current item: JACR - Just Another Checkbook Register