Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2. class user
  3. {
  4.     private $database = "sqlite:includes/database/users.sqlite";
  5.     private $db;
  6.     private $data = array();
  7.  
  8.  
  9.     public function __construct($identifier = NULL)
  10.     {
  11.         $this->db = new PDO($this->database);
  12.      
  13.         if (isset($identifier))
  14.             $this->load($identifier);
  15.     }
  16.    
  17.     public function __get($name)
  18.     {
  19.         return (isset($this->data[$name]) ? $this->data[$name] : false);
  20.     }
  21.    
  22.     public function __set($name, $value)
  23.     {
  24.         $this->data[$name] = $value;
  25.     }  
  26.  
  27.     public function add()
  28.     {
  29.         if (isset($this->data['id']))
  30.             return false;
  31.    
  32.         //diese hier brauchen wir unbedingt, sonst kann der user nicht geaddet werden
  33.         $required = array('name', 'email', 'password');
  34.         foreach ($required as $attribute)
  35.         {
  36.             if (!isset($this->data[$attribute]))
  37.                 return false;
  38.         }
  39.        
  40.         //Encrypt password
  41.         $this->data['password'] = md5($this->data['password']);
  42.        
  43.         //Prepare SQL query string
  44.         $attributes = '';
  45.         $values = '';
  46.         foreach ($this->data as $attribute => $value)
  47.         {
  48.             $attributes .= $attribute . ', ';
  49.             $values .= ':' . $attribute . ', ';
  50.         }  
  51.         $attributes = trim($attributes, ', ');
  52.         $values = trim($values, ', ');
  53.        
  54.         $query = 'INSERT INTO users (' . $attributes . ') VALUES (' . $values . ')';
  55.         var_dump($query);
  56.  
  57.         $db_prep = $this->db->prepare($query);
  58.         foreach ($this->data as $attribute => $value)
  59.         {
  60.             $db_prep->bindParam(':' . $attribute, $value);
  61.         }
  62.        
  63.         //ne gute idee um erfolg zu checken:
  64.         return $db_prep->execute();
  65.     }
  66.  
  67.     public function load($identifier)
  68.     {
  69.         if(is_numeric($identifier))
  70.         {
  71.             //Get assoc array
  72.             $db_prep = $this->db->prepare("SELECT * FROM users WHERE id = :id");
  73.             $db_prep->bindParam(":id",$identifier);
  74.             $db_prep->execute();
  75.             $this->data = $db_prep->fetch(PDO::FETCH_ASSOC);
  76.             return true;
  77.         }
  78.         else
  79.         {
  80.             return false;
  81.         }
  82.     }
  83.    
  84.     //sollte nur nach load ausgeführt werden
  85.     public function save()
  86.     {
  87.         if (!isset($this->data['id']))
  88.             return false;
  89.            
  90.         //Encrypt password
  91.         if (isset($this->data['password']))
  92.             $this->data['password'] = md5($this->data['password']);
  93.        
  94.         //Prepare SQL query string
  95.         $attributes = '';
  96.         foreach ($this->data as $attribute => $value)
  97.         {
  98.             $attributes .= $attribute . ' = :' . $attribute .  ', ';
  99.         }  
  100.         $attributes = trim($attributes, ', ');
  101.        
  102.         $query = 'UPDATE users SET ' . $attributes . ' WHERE id = ' . $this->data['id'];
  103.  
  104.         $db_prep = $this->db->prepare($query);
  105.         foreach ($this->data as $attribute => $value)
  106.         {
  107.             $db_prep->bindParam(':' . $attribute, $value);
  108.         }
  109.        
  110.         //ne gute idee um erfolg zu checken:
  111.         return $db_prep->execute();
  112.        
  113.     }
  114. }
  115.  
  116. error_reporting(E_ALL);
  117. ini_set('display_errors','On');
  118. $user = new user();
  119. $user->name = 'arez';
  120. $user->password = 'lalala';
  121. $user->email = 'noone@home.com';
  122. $user->age = '44';
  123.  
  124. var_dump($user->add());
  125.  
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement