Advertisement
Guest User

Untitled

a guest
May 7th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.35 KB | None | 0 0
  1. <?php
  2. class Model
  3. {
  4.     public $connection;
  5.    
  6.     private $_select;
  7.     private $_from;
  8.     private $_where;
  9.     private $_limit;
  10.     private $_orderBy;
  11.     private $_insertInto;
  12.     private $_update;
  13.     private $_remove;
  14.    
  15.     define("WHERE_OR", "OR");
  16.     define("WHERE_AND", "AND");
  17.    
  18.     function __construct()
  19.     {
  20.         $xml = simplexml_load_file("./app_code/config.xml");
  21.         $children = $xml->children();
  22.        
  23.         $dbHost = $children->database["host"];
  24.         $dbUser = $children->database["user"];
  25.         $dbPass = $children->database["password"];
  26.         $dbName = $children->database["name"];
  27.        
  28.         $this->connection = mysql_connect($dbHost, $dbUser, $dbPass) or die("Unable to connect to mysql database <br />". mysql_error());
  29.         mysql_select_db($dbName, $this->connection);
  30.     }
  31.    
  32.     public function select($select)
  33.     {
  34.         $this->_select = "SELECT ".$select;
  35.         return $this;
  36.     }
  37.    
  38.     public function from($from)
  39.     {
  40.         $this->_from = "FROM ".$from;
  41.         return $this;
  42.     }
  43.    
  44.     public function where($field, $value, $connector = WHERE_AND)
  45.     {
  46.         if($this->_where = "")
  47.         {
  48.             $this->_where = "WHERE `".$field."` = '".$value."'";
  49.         }
  50.         else
  51.         {
  52.             $this->_where = $connector." `".$field."` = '".$value."'";
  53.         }
  54.         return $this;
  55.     }
  56.    
  57.     public function limit($offset, $length = 0)
  58.     {
  59.         $this->_limit = "LIMIT ".$offset.", ".$length;
  60.         return $this;
  61.     }
  62.    
  63.     public function orderBy($by, $direction = "DESC")
  64.     {
  65.         $this->_orderBy = "ORDER BY ".$by." ".$direction;
  66.         return $this;
  67.     }
  68.    
  69.     public function InsertInto($tableName, $data)
  70.     {
  71.         $columnsResult = $this->GetTableFields($tableName);
  72.         $diff = array_diff($columnsResult, $data);
  73.        
  74.         if(count($diff) > 0)
  75.         {
  76.             die("The fields supplied must match the fields in the table");
  77.         }
  78.    
  79.         $this->_insertInto = "INSERT INTO `".$tableName."` (";
  80.        
  81.         $i = 1;
  82.         foreach($data as $key => $val)
  83.         {
  84.             if($i != count($data))
  85.             {
  86.                 $this->_insertInto .= "`".$key."`, ";
  87.             }
  88.             else
  89.             {
  90.                 $this->_insertInto .= "`".$key."`";
  91.             }
  92.             $i++;
  93.         }
  94.        
  95.         $this->_insertInto .= ") VALUES (";
  96.        
  97.         $i = 1;
  98.         foreach($data as $key => $val)
  99.         {
  100.             if($i != count($data))
  101.             {
  102.                 $this->_insertInto .= "'".$val."', ";
  103.             }
  104.             else
  105.             {
  106.                 $this->_insertInto .= "'".$val."'";
  107.             }
  108.             $i++;
  109.         }
  110.        
  111.         $this->_insertInto .= ")";
  112.        
  113.         $result = mysql_query($this->_insertInto);
  114.         $this->_insertInto = "";
  115.         return $result;
  116.     }
  117.    
  118.     public function Update($tableName, $data, $whereField = "", $whereValue = "")
  119.     {
  120.         $this->where($whereField, $whereValue);
  121.        
  122.         $columnsResult = $this->GetTableFields($tableName);
  123.        
  124.         $i = 1;
  125.         $this->_update = "UPDATE ".$tableName." SET ";
  126.         foreach($data as $key => $val)
  127.         {
  128.             if(!array_key_exists($key, $columnsResult))
  129.             {
  130.                 $this->_update = "";
  131.                 die("They field \"".$key."\" does not exist in the table \"".$tableName."\"");
  132.             }
  133.            
  134.             if($i < count($data))
  135.             {
  136.                 $this->_update .= "`".$key."` = '".$val."', ";
  137.             }
  138.             else
  139.             {
  140.                 $this->_update .= "`".$key."` = '".$val."' ";
  141.             }
  142.         }
  143.        
  144.         $this->_update .= $this->_where;
  145.  
  146.         $result = mysql_query($this->_update);
  147.         $this->_update = "";
  148.         $this->_where = "";
  149.        
  150.         return $result;
  151.        
  152.     }
  153.    
  154.     public function Remove($tableName, $field, $whereField = "", $whereValue = "")
  155.     {
  156.         $this->where($whereField, $whereValue);
  157.        
  158.         $dbFields = $this->GetTableFields($tableName);
  159.        
  160.         if(!array_key_exists($field, $dbFields))
  161.         {
  162.             die("The field \"".$field."\" is not a field in the table \"".$tableName."\"");
  163.         }
  164.        
  165.         $this->_remove = "DELETE FROM ".$tableName." WHERE ".$this->_where;
  166.        
  167.         $result = mysql_query($this->_remove);
  168.        
  169.         $this->_remove = "";
  170.         $this->_where = "";
  171.        
  172.         return $result;
  173.        
  174.     }
  175.    
  176.     public function GetResult()
  177.     {
  178.         $query = "".$this->_select." ".$this->_from." ".$this->_where." ".$this->_limit." ".$this->_orderBy."";
  179.         $result = mysql_query($query);
  180.        
  181.         $return = array();
  182.        
  183.         while($row = mysql_fetch_assoc($result))
  184.         {
  185.             $return[] = $row;
  186.         }
  187.            
  188.         $this->_select = "";
  189.         $this->_from = "";
  190.         $this->_where = "";
  191.         $this->_limit = "";
  192.         $this->_orderBy = "";
  193.    
  194.         return $return;
  195.     }
  196.    
  197.     private function GetTableFields($tableName)
  198.     {
  199.    
  200.         $columnsQuery = mysql_query("SHOW FIELDS FROM ".$tableName);
  201.         $columnsResult = array ();
  202.        
  203.         while($field = mysql_fetch_assoc($columnsQuery))
  204.         {
  205.             $columnsResult[$field["Field"]] = "";
  206.         }
  207.        
  208.         return $columnsResult;
  209.     }
  210. }
  211. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement