Advertisement
Guest User

Untitled

a guest
May 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 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.    
  13.     function __construct()
  14.     {
  15.         $xml = simplexml_load_file("./app_code/config.xml");
  16.         $children = $xml->children();
  17.        
  18.         $dbHost = $children->database["host"];
  19.         $dbUser = $children->database["user"];
  20.         $dbPass = $children->database["password"];
  21.         $dbName = $children->database["name"];
  22.        
  23.         $this->connection = mysql_connect($dbHost, $dbUser, $dbPass) or die("Unable to connect to mysql database <br />". mysql_error());
  24.         mysql_select_db($dbName, $this->connection);
  25.     }
  26.    
  27.     public function select($select)
  28.     {
  29.         $this->_select = "SELECT ".$select;
  30.         return $this;
  31.     }
  32.    
  33.     public function from($from)
  34.     {
  35.         $this->_from = "FROM ".$from;
  36.         return $this;
  37.     }
  38.    
  39.     public function where($where)
  40.     {
  41.         $this->_where = "WHERE ".$where;
  42.         return $this;
  43.     }
  44.    
  45.     public function limit($offset, $length = 0)
  46.     {
  47.         $this->_limit = "LIMIT ".$offset.", ".$length;
  48.         return $this;
  49.     }
  50.    
  51.     public function orderBy($by, $direction = "DESC")
  52.     {
  53.         $this->_orderBy = "ORDER BY ".$by." ".$direction;
  54.         return $this;
  55.     }
  56.    
  57.     public function InsertInto($tableName, $data)
  58.     {
  59.         $columnsQuery = mysql_query("SHOW FIELDS FROM ".$tableName);
  60.         $columnsResult = array ();
  61.        
  62.         while($field = mysql_fetch_assoc($columnsQuery))
  63.         {
  64.             $columnsResult[$field["Field"]] = "";
  65.         }
  66.        
  67.         $diff = array_diff($columnsResult, $data);
  68.        
  69.         if(count($diff) > 0)
  70.         {
  71.             die("The fields supplied must match the fields in the table");
  72.         }
  73.    
  74.         $this->_insertInto = "INSERT INTO `".$tableName."` (";
  75.        
  76.         $i = 1;
  77.         foreach($data as $key => $val)
  78.         {
  79.             if($i != count($data))
  80.             {
  81.                 $this->_insertInto .= "`".$key."`, ";
  82.             }
  83.             else
  84.             {
  85.                 $this->_insertInto .= "`".$key."`";
  86.             }
  87.             $i++;
  88.         }
  89.        
  90.         $this->_insertInto .= ") VALUES (";
  91.        
  92.         $i = 1;
  93.         foreach($data as $key => $val)
  94.         {
  95.             if($i != count($data))
  96.             {
  97.                 $this->_insertInto .= "'".$val."', ";
  98.             }
  99.             else
  100.             {
  101.                 $this->_insertInto .= "'".$val."'";
  102.             }
  103.             $i++;
  104.         }
  105.        
  106.         $this->_insertInto .= ")";
  107.        
  108.         $result = mysql_query($this->_insertInto);
  109.         $this->_insertInto = "";
  110.         return $result;
  111.     }
  112.    
  113.     public function GetResult()
  114.     {
  115.         $query = "".$this->_select." ".$this->_from." ".$this->_where." ".$this->_limit." ".$this->_orderBy."";
  116.         $result = mysql_query($query);
  117.        
  118.         $return = array();
  119.        
  120.         while($row = mysql_fetch_assoc($result))
  121.         {
  122.             $return[] = $row;
  123.         }
  124.            
  125.         $this->_select = "";
  126.         $this->_from = "";
  127.         $this->_where = "";
  128.         $this->_limit = "";
  129.         $this->_orderBy = "";
  130.    
  131.         return $return;
  132.     }
  133. }
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement