Advertisement
Shaun_B

blogpost.php for building the blog posts from the db

Oct 24th, 2012
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This will retrieve the various blog posts from the database, which
  4.  * is called from the 'Blogs' class (include.php).
  5.  * I will need a way to retrieve and edit blog posts, so more logic is
  6.  * needed.
  7.  * This is based on Ben Mills excellent tutorial found here:
  8.  * net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  9.  * This should go into the folder libraries and be called blogposts.php
  10.  * unless of course you want to change the file paths and names to make it more
  11.  * sense to you.
  12.  *
  13.  * @Author:     Shaun_B
  14.  * @Date:       2012-10-24
  15.  * @Todo:       More logic to retrieve and edit the blog posts
  16.  *
  17.  */
  18. include 'dateClass.php'; // Returns the date from a numberic format to various formats
  19. class BlogPost {
  20.     // Class-level variables:
  21.     public $id;
  22.     public $title;
  23.     public $post;
  24.     public $author;
  25.     public $url;
  26.     public $tags;
  27.     public $datePosted;
  28.     function __contruct() {
  29.         // Empty constructor
  30.         $id = null;
  31.         $title = null;
  32.         $post = null;
  33.         $author = null;
  34.         $url = null;
  35.         $tags = array();
  36.         $datePosted = new DateClass();
  37.     }
  38.     // This will construct the object:
  39.     function getPost($inId=null, $inTitle=null, $inPost=null,  $inAuthorId=null, $inUrl=null, $inDatePosted=null) {
  40.         // Gets blog id, title and post:
  41.         if (isset($inId)) {
  42.             $this->id = $inId;
  43.         }
  44.         if (isset($inTitle)) {
  45.             $this->title = $inTitle;
  46.         }
  47.         if (isset($inPost)) {
  48.             $this->post = $inPost;
  49.         }
  50.         // Inline SQL stuff:
  51.         $query = mysql_query ('SELECT date_posted FROM blog_posts WHERE id = ' . $inId);
  52.         $row = mysql_fetch_assoc ($query);
  53.         // Explodes date from database to an array, removing the dashes:
  54.         $inDatePosted = explode( '-', $row['date_posted'] );
  55.         // Sets new date and constructs it from the retrieved date from the database:
  56.         $date = new DateClass;
  57.         $date->setDate( $inDatePosted[2], $inDatePosted[1], $inDatePosted[0]-2000 );
  58.         $this->datePosted = $date->fullDate;
  59.         // Sets up author and URL from the database:
  60.         if (isset($inAuthorId)) {
  61.             $query = mysql_query("SELECT first_name, last_name, url FROM people WHERE id = " . $inAuthorId);
  62.             $row = mysql_fetch_assoc($query);
  63.             $this->author = $row['first_name'] . ' ' . $row['last_name'];
  64.             $this->url = '<abbr title="Visit ' . $this->author . '\'s website"> '
  65.                         . '<a href="' . $row['url'] . '">' . $row['url'] . '</a></abbr>';
  66.         }
  67.         // Sets up tags:
  68.         $postTags = 'No Tags';
  69.         if( isset( $inId ) ) {
  70.             $query = mysql_query( 'SELECT tags.* FROM blog_post_tags LEFT JOIN (tags)' .
  71.                     'ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = ' . $inId);
  72.             $tagArray = array();
  73.             $tagIDArray = array();
  74.             while($row = mysql_fetch_assoc($query)) {
  75.                 array_push($tagArray, $row["name"]);
  76.                 array_push($tagIDArray, $row["id"]);
  77.             }
  78.             if( sizeof( $tagArray ) > 0) {
  79.                 foreach ($tagArray as $tag) {
  80.                     if ($postTags == "No Tags") {
  81.                         $postTags = $tag;
  82.                     } else {
  83.                         $postTags = $postTags . ", " . $tag;
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.         $this->tags = '<abbr title="Search Google for ' . $postTags . '"> '
  89.                     . '<a href="https://www.google.co.uk/search?hl=en&safe=on&q=' . $postTags
  90.                     . '" target="_blank">' . $postTags . '</a> </abbr>';
  91.         return $this;
  92.     }
  93.     function __destruct() {
  94.         ;;
  95.     }
  96. }
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement