Advertisement
Shaun_B

Basic MCV framework example

Mar 16th, 2014
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.61 KB | None | 0 0
  1. <!-- VIEW -->
  2. <?php include_once '../controller/BlogPost.php'; ?>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6.     <link rel="stylesheet" href="assets/css/styles.css" type="text/css" media="screen" title="Blogging" charset="utf-8">
  7.     <title>Framework</title>
  8.     <meta name="description" content="Simple blogging site example">
  9.     <meta http-equiv="content-type" content="text/html;charset=UTF-8">
  10. </head>
  11. <body>
  12. <div id="main">
  13.     <h1>The Daily Blog</h1>
  14.     <div id="blogPosts" class="post">
  15.     <?php
  16.         $blogPosts  = new BlogPost;
  17.         $post       = $blogPosts->getPost(2);
  18.         // echo '<pre>' . print_r($post,true) . '</pre>';
  19.         $index      = 0;
  20.         while(!empty($post->post[$index])):
  21.     ?>
  22.     <h2><?php echo $post->title[$index]; ?></h2>
  23.         <p><?php echo $post->post[$index]; ?><br />
  24.             <span class="footer">Posted By: <?php echo $post->author; ?> || Website: <?php echo $post->url; ?> || Posted on: <?php echo $post->datePosted[$index]; ?>
  25.                 <?php
  26.                     if( !empty( $post->tags[$index]) ):
  27.                         echo ' || ' ;
  28.                         foreach($post->tags[$index] as $tags):
  29.                 ?>
  30.                
  31.                 <?php
  32.                             echo $tags . '&nbsp;';
  33.                         endforeach;
  34.                     endif;
  35.                     $index++;
  36.                     if($index>0):
  37.                 ?>
  38.                 <hr class="hr" />
  39.                 <?php
  40.                     endif;
  41.                 endwhile;
  42.                 ?>
  43.             </span>
  44.         </p>
  45.     </div>
  46. </div>
  47. </body>
  48. <footer>
  49. </footer>
  50. </html>
  51.  
  52.  
  53.  
  54. <!-- CONTROLLER -->
  55. <?php
  56. /**
  57.  * This will retrieve the various blog posts from the database, which
  58.  * is called from the 'Blogs' class (include.php).
  59.  * I will need a way to retrieve and edit blog posts, so more logic is
  60.  * needed.
  61.  * This is based on Ben Mills excellent tutorial found here:
  62.  * net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  63.  * This should go into the folder libraries and be called blogposts.php
  64.  * unless of course you want to change the file paths and names to make it more
  65.  * sense to you.
  66.  *
  67.  * @Author:     Shaun_B
  68.  * @Date:       2012-10-24
  69.  * @Todo:       More logic to retrieve and edit the blog posts
  70.  *
  71.  */
  72. include_once '../controller/DateHelper.php';
  73. include_once '../model/GetData.php';
  74. class BlogPost {
  75.     // Class-level variables:
  76.     public $title;
  77.     public $post;
  78.     public $author;
  79.     public $url;
  80.     public $tags;
  81.     public $datePosted;
  82.     function __contruct() {
  83.         $this->title = Array();
  84.         $this->post = Array();
  85.         $this->author = null;
  86.         $this->url = null;
  87.         $this->tags = array();
  88.         $this->datePosted = Array(new DateClass());
  89.     }
  90.     function getPost($uID = null) {
  91.         $blogPosts      = new GetData();
  92.         $key            = 'user_details';
  93.         $index          = 0;
  94.         $posts          = $blogPosts->getBlogPost($uID);
  95.         $this->author   = $posts[$key]['first_name'] . ' ' . $posts[$key]['last_name'];
  96.         $this->url      = '<abbr title="Visit ' . $this->author . '\'s website"><a href="' . $posts[$key]['url'] . '" target="_blank">' . $posts[$key]['url'] . '</a></abbr>';
  97.         $this->email    = $posts[$key]['email'];
  98.  
  99.         foreach ($posts['blog_post'] as $key => $post){
  100.             $this->title[$index] = $post['title'];
  101.             $this->post[$index] = $post['post'];
  102.             $inDatePosted= explode( '-', $post['date_posted'] );
  103.             $date = new DateHelper;
  104.             $date->setDate( $inDatePosted[2], $inDatePosted[1], $inDatePosted[0]-2000 );
  105.             $this->datePosted[$index] = $date->fullDate;
  106.             if( sizeof( $post['tags'] ) > 0) {
  107.                 foreach ($post['tags'] as $tags) {
  108.                     $this->tags[$index][] = '<abbr title="Search Google for ' . $tags . '"> '
  109.                         . '<a href="https://www.google.co.uk/search?hl=en&safe=on&q=' . $tags
  110.                         . '" target="_blank">' . $tags . '</a> </abbr>';               
  111.                 }
  112.             }
  113.             $index++;
  114.         }
  115.        
  116.         return $this;
  117.     }
  118.     function __destruct() {
  119.         ;;
  120.     }
  121. }
  122.  
  123.  
  124. <!-- MODEL -->
  125. <?php
  126. /**
  127.  * Original source:
  128.  * net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  129.  * New version uses PDO objects as my_sql is deprecated
  130.  *
  131.  * @author:     Shaun B
  132.  * @date:       2012-10-24 | 2014-03-15
  133.  * @todo:      
  134.  */
  135. class GetData {
  136.     public $connection;
  137.     public $user;
  138.     public $password;
  139.     public $databaseHandle;
  140.     public $result;
  141.    
  142.     function __construct(){
  143.         $this->connection = 'mysql:dbname=blog_db;host=127.0.0.1;';
  144.         $this->user = 'admin';
  145.         $this->password = 'password';
  146.         try {
  147.             $this->databaseHandle = new PDO($this->connection, $this->user, $this->password);
  148.             $this->databaseHandle->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
  149.         } catch (PDOException $e) {
  150.             die ('Connection failed: ' . $e->getMessage());
  151.         }
  152.         $this->result = Array();
  153.     }
  154.     /**
  155.      * Here's our querys as functions
  156.      *
  157.      * @author:     Shaun B
  158.      * @date:       2014-03-15
  159.      * @todo:      
  160.      */
  161.     function getBlogPost($userID = null) {
  162.         $postID = Array();
  163.         if ($userID != null) {
  164.             $_result = $this->getUser($userID);
  165.             if(is_array($_result)){
  166.                 $this->result['user_details'] =  $_result;
  167.             }
  168.             $_result = $this->getBlog($userID);
  169.             if(is_array($_result)){
  170.                 $this->result['blog_post'] = $_result;
  171.             }
  172.         }
  173.         return array_reverse($this->result);
  174.     }
  175.     /**
  176.      * Here's our query to get the user details
  177.      *
  178.      * @author:     Shaun B
  179.      * @date:       2014-03-15
  180.      * @todo:
  181.      */
  182.     function getUser($userID = null){
  183.         $query = $this->databaseHandle->query( "SELECT `first_name`, `last_name`, `url`, `email` FROM `people` WHERE id = " . $userID );
  184.         return $query->fetch(PDO::FETCH_ASSOC);
  185.     }
  186.     /**
  187.      * Here's our query to get the user details
  188.      *
  189.      * @author:     Shaun B
  190.      * @date:       2014-03-15
  191.      * @todo:
  192.      */
  193.     function getBlog($userID = null){
  194.         $result = Array();
  195.         $query = $this->databaseHandle->query( "SELECT `id`, `title`, `post`, `date_posted` FROM `blog_posts` WHERE `author_id` = " . $userID );
  196.         foreach($query as $key => $data){
  197.             $result[$key] = $data;
  198.             $result[$key]['tags'] = $this->getTagID($data['id']);
  199.         }
  200.         return $result;
  201.     }
  202.     /**
  203.      * This will build the array of tags by the user
  204.      *
  205.      * @author:     Shaun B
  206.      * @date:       2014-03-16
  207.      * @todo:
  208.      */
  209.     function getTagID($pID = null){
  210.         $result = Array();
  211.         $query = $this->databaseHandle->query( "SELECT `tag_id` FROM `blog_post_tags` WHERE `blog_post_id` = " . $pID );
  212.         foreach($query as $key => $data){
  213.             $result[$key] = $this->getTag($data['tag_id']);
  214.         }
  215.         return $result;
  216.     }
  217.     /**
  218.      * And this will return individual tags
  219.      *
  220.      * @author:     Shaun B
  221.      * @date:       2014-03-16
  222.      * @todo:
  223.      */
  224.     function getTag($tID = null){
  225.         $query = $this->databaseHandle->query( "SELECT `name` FROM `tags` WHERE `id` = " . $tID);
  226.         $query = $query->fetch(PDO::FETCH_ASSOC);
  227.         return $query['name'];
  228.     }
  229.     /**
  230.      * Destruct function closes database connection
  231.      *
  232.      * @author:     Shaun B
  233.      * @date:       2014-03-16
  234.      * @todo:
  235.      */
  236.     function __destruct() {
  237.         $this->databaseHandle = null;
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement