Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by JetBrains PhpStorm.
  4.  * User: Administrator
  5.  * Date: 11/07/11
  6.  * Time: 20:33
  7.  * To change this template use File | Settings | File Templates.
  8.  */
  9.  
  10. abstract class DatabaseModel {
  11.  
  12.     var $tablename; // table name
  13.     var $dbname; // database name
  14.     var $rows_per_page; // used in pagination
  15.     var $pageno; // current page number
  16.     var $lastpage; // highest page number
  17.     var $fieldlist; // list of fields in this table
  18.     var $data_array; // data from the database
  19.     var $errors; // array of error messages
  20.     var $numrows;
  21.  
  22.      //todo rewrite so everything can be used.
  23.     function getData($where) {
  24.         $this->data_array = array();
  25.         $pageno = $this->pageno;
  26.         $rows_per_page = $this->rows_per_page;
  27.         $this->numrows = 0;
  28.         $this->lastpage = 0;
  29.  
  30.         if (empty($where)) {
  31.             $where_str = NULL;
  32.         } else {
  33.             $where_str = "WHERE $where";
  34.         } // if
  35.         $this->numrows = SQL::query("SELECT count(*) as
  36.                `numrows`FROM $this->tablename $where_str")->fetchField('numrows');
  37.         if ($this->numrows <= 0) {
  38.             $this->pageno = 0;
  39.             return;
  40.         } // if
  41.         if ($rows_per_page > 0) {
  42.             $this->lastpage = ceil($this->numrows / $rows_per_page);
  43.         } else {
  44.             $this->lastpage = 1;
  45.         } // if
  46.         if ($pageno == '' OR $pageno <= '1') {
  47.             $pageno = 1;
  48.         } elseif ($pageno > $this->lastpage) {
  49.             $pageno = $this->lastpage;
  50.         } // if
  51.         $this->pageno = $pageno;
  52.  
  53.  
  54.     }
  55.  
  56.     public function __construct($tablename) {
  57.         /**
  58.          * fieldlist
  59.          */
  60.         $collums = SQL::query("SELECT * FROM `db_schema`
  61.                               WHERE `TABLE_NAME` = '$tablename'");
  62.         while ($collums->isValid()) {
  63.             $this->fieldlist[] = $collums->fetchField('COLUMN_NAME');
  64.             $key = $collums->fetchField('COLUMN_KEY');
  65.             if (!empty($key)) {
  66.                 $this->fieldlist[$collums->fetchField('COLUMN_NAME')] = array($key => 'y');
  67.             }
  68.             $collums->next();
  69.         }
  70.  
  71.  
  72.     }
  73.  
  74.     public function _print($key, $values, $indent = 0) {
  75.         if (is_array($values)) {
  76.             $indent++;
  77.             foreach ($values as $key => $value)
  78.                 $this->_print($key, $value, $indent);
  79.         } else {
  80.             for ($i = 0; $i < $indent; $i++)
  81.                 echo "\t";
  82.             echo "-$key : $values\n<br>";
  83.         }
  84.  
  85.     }
  86.  
  87.     protected function _insert($list) {
  88.         $query = "INSERT INTO `{$this->tablename}` SET";
  89.         foreach ($list as $key => $value)
  90.             $query .= "$key = " . (is_numeric($value) ? $value : "'$value'") . " ,";
  91.         $query = rtrim($query, ', ');
  92.         SQL::query($query);
  93.         return;
  94.     }
  95.  
  96.     protected function _update($list) {
  97.  
  98.         foreach ($list as $field => $fieldvalue) {
  99.             if (!in_array($field, $this->fieldlist)) {
  100.                 unset ($list[$field]);
  101.  
  102.             }
  103.         }
  104.         $where = NULL;
  105.         $update = NULL;
  106.         foreach ($list as $item => $value) {
  107.             if (isset($fieldlist[$item]['PRI'])) {
  108.                 $where .= "$item='$value' AND ";
  109.             } else {
  110.                 $update .= "$item='$value', ";
  111.             }
  112.         }
  113.         $where = rtrim($where, ' AND ');
  114.         $update = rtrim($update, ', ');
  115.         SQL::execute("UPDATE $this->tablename SET $update WHERE $where");
  116.     }
  117.  
  118.     protected function _delete($list) {
  119.         $where = NULL;
  120.         foreach ($list as $item => $value) {
  121.             if (isset($this->fieldlist[$item]['PRI'])) {
  122.                 $where .= "$item='$value' AND ";
  123.             } // if
  124.         } // foreach
  125.         $where = rtrim($where, ' AND ');
  126.         SQL::execute("DELETE FROM $this->tablename WHERE $where");
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement