Advertisement
Esforeal

Simple MVC

Oct 17th, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. /** Model (../models/Category.php)*/
  2. <?php
  3. class Category
  4. {
  5.     public static function getList()
  6.     {
  7.         try
  8.         {
  9.             $db = Db::getConnection();
  10.  
  11.             $categories = $db->prepare("SELECT * FROM categories");
  12.             $categories->execute();
  13.  
  14.             return $categories->fetchAll(PDO::FETCH_ASSOC);
  15.         } catch (PDOException $err)
  16.         {
  17.             die("I can't get category list, because " . $err->getMessage());
  18.         }
  19.     }
  20. }
  21. ?>
  22.  
  23. /** Controller (../controllers/AdminController.php)*/
  24. <?php
  25. class AdminController
  26. {
  27.     public function actionIndex()
  28.     {
  29.         $categories = Category::getList();
  30.        
  31.         require_once ROOT . '/views/pages/admin/admin.php';
  32.  
  33.         return true;
  34.     }
  35. }
  36. ?>
  37.  
  38. /** Somewhere in View */
  39.  
  40. ...
  41. <?php foreach ($categories as $category): ?>
  42.     blablabla <?=$category['category_name']; ?>
  43.     blablabla
  44. <?php endforeach; ?>
  45. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement