Advertisement
Guest User

Rewrite

a guest
Apr 16th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <?php
  2. class News
  3. {
  4.  
  5.   private $host = 'localhost';
  6.   private $dbname = 'mvc_site';
  7.   private $user = 'root';
  8.   private $password = '';
  9.   private $db;
  10.  
  11.  
  12.     public function __construct()
  13.     {
  14.       $this -> db = new PDO("mysql:host = $host; dbname = $dbname", $user, $password);
  15.     }
  16.  
  17.     public function getNewsItemById($id)
  18.     {
  19.        $id = intval($id);
  20.        
  21.         if($id)
  22.         {
  23.        
  24.         $newsList = [];
  25.        
  26.         $result = $this -> db->query('SELECT * FROM news WHERE id='.$id) -> fetchAll()[0];
  27.  
  28.         return $newsItem;    
  29.  
  30.         }
  31.        
  32.     }
  33.    
  34.    
  35.     public static function getNewsList()
  36.     {
  37.        
  38.         $newsList = [];
  39.        
  40.         $result = $this -> db->query('SELECT id, title, date, short_content'
  41.                              .'FROM news'
  42.                              .'ORDER BY date DESC'
  43.                              .'LIMIT 10') -> fetchAll();
  44.                
  45.         return array_merge($result,$newList);
  46.     }
  47. }
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement