Advertisement
skisr2000

user.php

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