Advertisement
Guest User

Untitled

a guest
May 6th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.48 KB | None | 0 0
  1. <?php
  2. class Model
  3. {
  4.     private $db = null;
  5.     protected $connection = null;
  6.     public $instance = null;
  7.  
  8.     public function __construct() {
  9.         $this->load_db('stat');
  10.     }
  11.    
  12.     // Set session
  13.     public function set_session($session_name, $value)
  14.     {
  15.         $_SESSION[$session_name] = $value;
  16.     }
  17.    
  18.     // Fetch session
  19.     public function session($session_name)
  20.     {
  21.         return $_SESSION[$session_name];
  22.     }
  23.    
  24.     public function load_db($name)
  25.     {
  26.         // Include db file
  27.         include('config.php');
  28.      
  29.        // echo $name;
  30.      
  31.         if($database[$name] != NULL)
  32.         {
  33.             $this->db = new mysqli($database[$name]['host'], $database[$name]['username'], $database[$name]['password'], $database[$name]['database']);
  34.             //echo 'Connected to new database!';
  35.         }
  36.         else
  37.         {
  38.             die('The connection named : ' . $name . ' can not be found in the configurations.');
  39.         }
  40.     }
  41.    
  42.     public function load($name)
  43.     {
  44.         if($name != NULL)
  45.         {
  46.             if(file_exists(MODELS_PATH.$name.'.php'))
  47.             {
  48.                 $loaded_model = include(MODELS_PATH.$name.'.php');
  49.                 //$this->instance = new $name();
  50.                 $this->instance = new $name();
  51.                 return new $name();
  52.             }
  53.             else
  54.             {
  55.                 die('The model ' . $name . ' can not be found!');
  56.             }
  57.         }
  58.     }
  59.    
  60.     //query->("SELECT");
  61.     public function fetch_assoc() {
  62.         if($this->instance) {
  63.  
  64.             return $this->instance->fetch_assoc();
  65.         }
  66.         else
  67.         {
  68.             echo 'fail fetch assoc';
  69.         }        
  70.     }
  71.    
  72.     public function query($query) {
  73.         $sql = $query;
  74.         $this->instance = $this->db->query($sql);
  75.        
  76.         return $this->db->query($sql);
  77.     }
  78.    
  79.     public function select($query) {
  80.         $sql = $query;
  81.         //echo $sql;
  82.         $this->instance = $this->query($sql);
  83.         //echo $this->db->error;
  84.        
  85.         //print_r($this->instance);
  86.        
  87.         return $this->instance;
  88.         // $this->query()->num_rows();
  89.         // $this->query()
  90.         // $this->num
  91.         //return $this;
  92.     }
  93.    
  94.     public function num_rows()
  95.     {
  96.         if ( $this->instance ) {
  97.             return $this->instance->num_rows;
  98.             //return num_rows;
  99.         } else {
  100.            
  101.         }
  102.     }
  103. }
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement