Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- VIEW -->
- <?php include_once '../controller/BlogPost.php'; ?>
- <!DOCTYPE html>
- <html>
- <head>
- <link rel="stylesheet" href="assets/css/styles.css" type="text/css" media="screen" title="Blogging" charset="utf-8">
- <title>Framework</title>
- <meta name="description" content="Simple blogging site example">
- <meta http-equiv="content-type" content="text/html;charset=UTF-8">
- </head>
- <body>
- <div id="main">
- <h1>The Daily Blog</h1>
- <div id="blogPosts" class="post">
- <?php
- $blogPosts = new BlogPost;
- $post = $blogPosts->getPost(2);
- // echo '<pre>' . print_r($post,true) . '</pre>';
- $index = 0;
- while(!empty($post->post[$index])):
- ?>
- <h2><?php echo $post->title[$index]; ?></h2>
- <p><?php echo $post->post[$index]; ?><br />
- <span class="footer">Posted By: <?php echo $post->author; ?> || Website: <?php echo $post->url; ?> || Posted on: <?php echo $post->datePosted[$index]; ?>
- <?php
- if( !empty( $post->tags[$index]) ):
- echo ' || ' ;
- foreach($post->tags[$index] as $tags):
- ?>
- <?php
- echo $tags . ' ';
- endforeach;
- endif;
- $index++;
- if($index>0):
- ?>
- <hr class="hr" />
- <?php
- endif;
- endwhile;
- ?>
- </span>
- </p>
- </div>
- </div>
- </body>
- <footer>
- </footer>
- </html>
- <!-- CONTROLLER -->
- <?php
- /**
- * This will retrieve the various blog posts from the database, which
- * is called from the 'Blogs' class (include.php).
- * I will need a way to retrieve and edit blog posts, so more logic is
- * needed.
- * This is based on Ben Mills excellent tutorial found here:
- * net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
- * This should go into the folder libraries and be called blogposts.php
- * unless of course you want to change the file paths and names to make it more
- * sense to you.
- *
- * @Author: Shaun_B
- * @Date: 2012-10-24
- * @Todo: More logic to retrieve and edit the blog posts
- *
- */
- include_once '../controller/DateHelper.php';
- include_once '../model/GetData.php';
- class BlogPost {
- // Class-level variables:
- public $title;
- public $post;
- public $author;
- public $url;
- public $tags;
- public $datePosted;
- function __contruct() {
- $this->title = Array();
- $this->post = Array();
- $this->author = null;
- $this->url = null;
- $this->tags = array();
- $this->datePosted = Array(new DateClass());
- }
- function getPost($uID = null) {
- $blogPosts = new GetData();
- $key = 'user_details';
- $index = 0;
- $posts = $blogPosts->getBlogPost($uID);
- $this->author = $posts[$key]['first_name'] . ' ' . $posts[$key]['last_name'];
- $this->url = '<abbr title="Visit ' . $this->author . '\'s website"><a href="' . $posts[$key]['url'] . '" target="_blank">' . $posts[$key]['url'] . '</a></abbr>';
- $this->email = $posts[$key]['email'];
- foreach ($posts['blog_post'] as $key => $post){
- $this->title[$index] = $post['title'];
- $this->post[$index] = $post['post'];
- $inDatePosted= explode( '-', $post['date_posted'] );
- $date = new DateHelper;
- $date->setDate( $inDatePosted[2], $inDatePosted[1], $inDatePosted[0]-2000 );
- $this->datePosted[$index] = $date->fullDate;
- if( sizeof( $post['tags'] ) > 0) {
- foreach ($post['tags'] as $tags) {
- $this->tags[$index][] = '<abbr title="Search Google for ' . $tags . '"> '
- . '<a href="https://www.google.co.uk/search?hl=en&safe=on&q=' . $tags
- . '" target="_blank">' . $tags . '</a> </abbr>';
- }
- }
- $index++;
- }
- return $this;
- }
- function __destruct() {
- ;;
- }
- }
- <!-- MODEL -->
- <?php
- /**
- * Original source:
- * net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
- * New version uses PDO objects as my_sql is deprecated
- *
- * @author: Shaun B
- * @date: 2012-10-24 | 2014-03-15
- * @todo:
- */
- class GetData {
- public $connection;
- public $user;
- public $password;
- public $databaseHandle;
- public $result;
- function __construct(){
- $this->connection = 'mysql:dbname=blog_db;host=127.0.0.1;';
- $this->user = 'admin';
- $this->password = 'password';
- try {
- $this->databaseHandle = new PDO($this->connection, $this->user, $this->password);
- $this->databaseHandle->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
- } catch (PDOException $e) {
- die ('Connection failed: ' . $e->getMessage());
- }
- $this->result = Array();
- }
- /**
- * Here's our querys as functions
- *
- * @author: Shaun B
- * @date: 2014-03-15
- * @todo:
- */
- function getBlogPost($userID = null) {
- $postID = Array();
- if ($userID != null) {
- $_result = $this->getUser($userID);
- if(is_array($_result)){
- $this->result['user_details'] = $_result;
- }
- $_result = $this->getBlog($userID);
- if(is_array($_result)){
- $this->result['blog_post'] = $_result;
- }
- }
- return array_reverse($this->result);
- }
- /**
- * Here's our query to get the user details
- *
- * @author: Shaun B
- * @date: 2014-03-15
- * @todo:
- */
- function getUser($userID = null){
- $query = $this->databaseHandle->query( "SELECT `first_name`, `last_name`, `url`, `email` FROM `people` WHERE id = " . $userID );
- return $query->fetch(PDO::FETCH_ASSOC);
- }
- /**
- * Here's our query to get the user details
- *
- * @author: Shaun B
- * @date: 2014-03-15
- * @todo:
- */
- function getBlog($userID = null){
- $result = Array();
- $query = $this->databaseHandle->query( "SELECT `id`, `title`, `post`, `date_posted` FROM `blog_posts` WHERE `author_id` = " . $userID );
- foreach($query as $key => $data){
- $result[$key] = $data;
- $result[$key]['tags'] = $this->getTagID($data['id']);
- }
- return $result;
- }
- /**
- * This will build the array of tags by the user
- *
- * @author: Shaun B
- * @date: 2014-03-16
- * @todo:
- */
- function getTagID($pID = null){
- $result = Array();
- $query = $this->databaseHandle->query( "SELECT `tag_id` FROM `blog_post_tags` WHERE `blog_post_id` = " . $pID );
- foreach($query as $key => $data){
- $result[$key] = $this->getTag($data['tag_id']);
- }
- return $result;
- }
- /**
- * And this will return individual tags
- *
- * @author: Shaun B
- * @date: 2014-03-16
- * @todo:
- */
- function getTag($tID = null){
- $query = $this->databaseHandle->query( "SELECT `name` FROM `tags` WHERE `id` = " . $tID);
- $query = $query->fetch(PDO::FETCH_ASSOC);
- return $query['name'];
- }
- /**
- * Destruct function closes database connection
- *
- * @author: Shaun B
- * @date: 2014-03-16
- * @todo:
- */
- function __destruct() {
- $this->databaseHandle = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement