earthtoph0601

PHP

Jun 25th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. DATABASE-OBJECTS.PHP
  2.  
  3. <?php
  4. require_once('initialize.php');
  5.  
  6. class DatabaseObjects{
  7.    
  8.     private $dbh;
  9.     static $table_name;
  10.     static $db_fields;
  11.    
  12.     public function __construct(){
  13.         global $MySQLDatabase;
  14.         global $news;
  15.        
  16.         $this->dbh = $MySQLDatabase->open_connection();
  17.        
  18.         // Convert Table Fields in to Attributes
  19.         static::$db_fields = $this->get_dbFields(static::$table_name);
  20.         foreach(static::$db_fields as $field){
  21.             $this->$field = "";
  22.         }
  23.     }
  24.    
  25.     // Get Table Fields from the Database
  26.     public function get_dbFields($table_name){
  27.         $sql = 'DESCRIBE ' . $table_name ;
  28.         $query = $this->dbh->prepare($sql);
  29.         $query->execute();
  30.         $result = $query->fetchAll(PDO::FETCH_OBJ);
  31.  
  32.         // Get the quantity of the Table Columns
  33.         $count = count($result);
  34.         $field = array();
  35.        
  36.         // Loop through each column to get the 'Field'
  37.         // ($count - 1) -> $count is minus 1 because array count starts with zero
  38.             // Example : Table Have 8 Columns
  39.             //           $count will read it as 1 - 8 while array ($result[i]) reads it as 0 - 7
  40.         for($i = 0; $i <= ($count - 1); $i++){
  41.             $field[] = $result[$i]->Field;
  42.         }
  43.         return $field;
  44.     }
  45.    
  46.     public function find_all(){
  47.         $sql = 'SELECT * FROM ' . static::$table_name;
  48.         return $this->find_by_sql($sql);
  49.     }
  50.    
  51.     private function find_by_sql($sql){
  52.         $query  = $this->dbh->prepare($sql);
  53.         $query->execute();
  54.        
  55.         $array_object = array();
  56.         while($rows = $query->fetch(PDO::FETCH_ASSOC)){
  57.             $array_object[] = $this->instantiate($rows);
  58.         }
  59.         return $array_object;
  60.     }
  61.    
  62.     private function instantiate($record){
  63.         $object = new self;
  64.         foreach($record as $attribute => $value){
  65.             if($this->has_attribute($attribute)){
  66.                 $object->$attribute = $value;
  67.             }
  68.         }
  69.        
  70.         return $object;
  71.     }
  72.    
  73.     private function has_attribute($attribute){
  74.         return array_key_exists($attribute, $this->attributes());
  75.     }
  76.    
  77.     private function attributes(){
  78.         $attributes = array();
  79.        
  80.         foreach(static::$db_fields as $field){
  81.             $attributes[$field] = $field;
  82.         }
  83.        
  84.         return $attributes;
  85.     }
  86. }
  87.  
  88. ?>
  89. -----------------------------------------------
  90. NEWS-CLASS.PHP
  91.  
  92. <?php
  93.    
  94. require_once('initialize.php');
  95.    
  96. class News extends DatabaseObjects{
  97.    
  98.     static $table_name = 'news';
  99.     static $db_fields;
  100.    
  101. }
  102.  
  103. $news = new News();
  104.  
  105. ?>
  106. ----------------------------------------------
  107.  
  108. NEWS.PHP
  109.  
  110. <?php include('../includes/initialize.php');
  111.  
  112.     $r =  $news->find_all();
  113.     foreach($r as $a){
  114.         echo '<h1>'.$a->title.'</h1>';
  115.     }
  116. ?>
Advertisement
Add Comment
Please, Sign In to add comment