Guest User

Untitled

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