Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. <?php
  2. include_once 'connection.php';
  3. class DbModels
  4. {
  5.  
  6.     protected $dbTableName;
  7.     protected $sort;
  8.     protected $dbTableParam;
  9.     function __construct(){
  10.         //$x = include '../configs/config.php';
  11.         $this->conn = new DataBase('mysql.hostinger.pl','u322614235_admin','zaq1@WSX','u322614235_prime');
  12.     }
  13.  
  14.  
  15.  
  16.  
  17.     public function add($data){
  18.         $i ='';
  19.         $v ='';
  20.         foreach ($data as $item=>$value ) {
  21.             $i .= $item.",";
  22.             $v .= "'".$value."',";
  23.         }
  24.         $i = substr($i, 0, -1);
  25.         $v = substr($v, 0, -1);
  26.         $sql = "INSERT INTO $this->dbTableName ($i) VALUES ($v)";
  27.         if ($this->conn->query($sql)){
  28.                 return true;
  29.             }
  30.             echo $sql;
  31.     }
  32.  
  33.     public function edit($data){
  34.         $x ='';
  35.         if(!key_exists('id',$data)){
  36.             throw new Exception('Brak identyfikatora');
  37.         }
  38.  
  39.         foreach ($data as $item=>$value ) {
  40.             $x .= $item."='".$value."',";
  41.         }
  42.         $x = substr($x, 0, -1);
  43.         $sql="UPDATE $this->dbTableName SET $x WHERE id=$data[id]";
  44.         if ($this->conn->query($sql)){
  45.             return true;
  46.         }
  47.     }
  48.  
  49.     public function delete($id){
  50.  
  51.         $sql="DELETE FROM $this->dbTableName WHERE id=$id";
  52.         if ($this->conn->query($sql)){
  53.             return true;
  54.         }
  55.     }
  56.  
  57.     public function getList(){
  58.         $sql = "SELECT * FROM $this->dbTableName ORDER BY $this->sort";
  59.         $return = $this->conn->queryAndRows($sql);
  60.         return $return;
  61.     }
  62.  
  63.      public function getOne($item,$value){
  64.  
  65.         $sql = "SELECT * FROM $this->dbTableName WHERE $item = LCASE('$value')";
  66.         $return = $this->conn->queryAndRow($sql);
  67.         return $return;
  68.     }
  69.     public function getOneA($item,$value,$item2,$value2){
  70.  
  71.         $sql = "SELECT * FROM $this->dbTableName WHERE $item = LCASE('$value') AND $item2 =  LCASE('$value2')";
  72.         $return = $this->conn->queryAndRow($sql);
  73.         return $return;
  74.     }
  75.     public function getConnList($id){
  76.         $sql = "
  77.          select
  78.            *
  79.          from
  80.            user$this->dbTableName as f
  81.            join $this->dbTableName as s on f.$this->dbTableParam = s.id
  82.            left join users as u on u.id =  f.userID
  83.          where
  84.            u.id = $id
  85.            
  86.            ";
  87.         $return = $this->conn->queryAndRows($sql);
  88.         return $return;
  89.     }
  90.     public function deleteConn($idU,$idM){
  91.         $sql="DELETE FROM user$this->dbTableName WHERE userID=$idU AND $this->dbTableParam=$idM";
  92.         if ($this->conn->query($sql)){
  93.             return true;
  94.         }
  95.     }
  96.  
  97.     public function addConn($idU,$idM){
  98.         $sql = "INSERT INTO user$this->dbTableName (userID,$this->dbTableParam) VALUES ($idU,$idM)" ;
  99.         if ($this->conn->query($sql)){
  100.             return true;
  101.         }
  102.         echo $sql;
  103.     }
  104.  
  105.     public function getOneConn($item,$value,$item2,$value2){
  106.  
  107.         $sql = "SELECT * FROM user$this->dbTableName WHERE $item = LCASE('$value') AND $item2 =  LCASE('$value2')";
  108.         $return = $this->conn->queryAndRow($sql);
  109.         return $return;
  110.  
  111.  
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement