Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by JetBrains PhpStorm.
- * User: Administrator
- * Date: 11/07/11
- * Time: 20:33
- * To change this template use File | Settings | File Templates.
- */
- abstract class DatabaseModel {
- 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 $numrows;
- //todo rewrite so everything can be used.
- function getData($where) {
- $this->data_array = array();
- $pageno = $this->pageno;
- $rows_per_page = $this->rows_per_page;
- $this->numrows = 0;
- $this->lastpage = 0;
- if (empty($where)) {
- $where_str = NULL;
- } else {
- $where_str = "WHERE $where";
- } // if
- $this->numrows = SQL::query("SELECT count(*) as
- `numrows`FROM $this->tablename $where_str")->fetchField('numrows');
- 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;
- }
- public function __construct($tablename) {
- /**
- * fieldlist
- */
- $collums = SQL::query("SELECT * FROM `db_schema`
- WHERE `TABLE_NAME` = '$tablename'");
- while ($collums->isValid()) {
- $this->fieldlist[] = $collums->fetchField('COLUMN_NAME');
- $key = $collums->fetchField('COLUMN_KEY');
- if (!empty($key)) {
- $this->fieldlist[$collums->fetchField('COLUMN_NAME')] = array($key => 'y');
- }
- $collums->next();
- }
- }
- public function _print($key, $values, $indent = 0) {
- if (is_array($values)) {
- $indent++;
- foreach ($values as $key => $value)
- $this->_print($key, $value, $indent);
- } else {
- for ($i = 0; $i < $indent; $i++)
- echo "\t";
- echo "-$key : $values\n<br>";
- }
- }
- protected function _insert($list) {
- $query = "INSERT INTO `{$this->tablename}` SET";
- foreach ($list as $key => $value)
- $query .= "$key = " . (is_numeric($value) ? $value : "'$value'") . " ,";
- $query = rtrim($query, ', ');
- SQL::query($query);
- return;
- }
- protected function _update($list) {
- foreach ($list as $field => $fieldvalue) {
- if (!in_array($field, $this->fieldlist)) {
- unset ($list[$field]);
- }
- }
- $where = NULL;
- $update = NULL;
- foreach ($list as $item => $value) {
- if (isset($fieldlist[$item]['PRI'])) {
- $where .= "$item='$value' AND ";
- } else {
- $update .= "$item='$value', ";
- }
- }
- $where = rtrim($where, ' AND ');
- $update = rtrim($update, ', ');
- SQL::execute("UPDATE $this->tablename SET $update WHERE $where");
- }
- protected function _delete($list) {
- $where = NULL;
- foreach ($list as $item => $value) {
- if (isset($this->fieldlist[$item]['PRI'])) {
- $where .= "$item='$value' AND ";
- } // if
- } // foreach
- $where = rtrim($where, ' AND ');
- SQL::execute("DELETE FROM $this->tablename WHERE $where");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement