Advertisement
xiaozhou0211

user.php

Sep 10th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. <?php
  2.  
  3. class User{
  4.  
  5.     public $id;
  6.     public $username;
  7.     public $password;
  8.     public $first_name;
  9.     public $last_name;
  10.  
  11.     public static function find_all_users(){
  12.  
  13.         return self::find_this_query("SELECT * FROM users");
  14.     }
  15.  
  16.     public static function find_user_id($user_id){
  17.  
  18.         global $database;
  19.  
  20.  
  21.        
  22.  
  23.         // $found_user = mysqli_fetch_array($result_user_id);
  24.         return $found_user;
  25.     }
  26.  
  27.     public static function find_this_query($sql){
  28.  
  29.         global $database;
  30.  
  31.         $result_set = $database->query($sql);
  32.  
  33.         $the_object_array = array();
  34.        
  35.         while($row = mysqli_fetch_array($result_set)){
  36.             $the_object_array[] = self::instantation($row);
  37.         }
  38.         return $the_object_array;
  39.     }
  40.  
  41.     public static function verify_user($username,$password){
  42.         global $database;
  43.  
  44.         $username = $database->escape_string($username);
  45.         $password = $database->escape_string($password);
  46.        
  47.         $sql = "SELECT * FROM users WHERE ";
  48.  
  49.         // $sql = "SELECT * FROM users WHERE";
  50.         $sql .= "username = '{$username}' ";
  51.         $sql .= "AND password = '{$password}' ";
  52.         $sql .= "LIMIT 1";
  53.         $the_result_array = self::find_this_query($sql);
  54.  
  55.         // if(!empty($the_result_array)){
  56.         //  $first_item = array_shift($the_result_array);
  57.         //  return $first_item;
  58.         // }else{
  59.         //  return false;
  60.         // }
  61.  
  62.         //Ternary operator: (condition)?(true return value):(false return value)
  63.  
  64.         return !empty($the_result_array) ? array_shift($the_result_array) :false;
  65.  
  66.     }
  67.  
  68.     public static function instantation($the_record){
  69.  
  70.         $the_object = new self;
  71.  
  72.         // $the_object->id = $found_user['id'];
  73.         // $the_object->username = $found_user['username'];
  74.         // $the_object->password = $found_user['password'];
  75.         // $the_object->first_name = $found_user['first_name'];
  76.         // $the_object->last_name = $found_user['last_name'];
  77.  
  78.         foreach ($the_record as $the_attribute => $value) {
  79.  
  80.             if($the_object->has_the_atrribute($the_attribute)){
  81.  
  82.                 $the_object->$the_attribute = $value;
  83.             }
  84.         }
  85.         return $the_object;
  86.     }
  87.  
  88.     private function has_the_atrribute($the_attribute){
  89.  
  90.         $object_properties = get_object_vars($this);
  91.  
  92.         return array_key_exists($the_attribute, $object_properties);
  93.     }
  94.  
  95.  
  96.  
  97.  
  98.    
  99. }
  100.  
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement