Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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 'dateClass.php'; // Returns the date from a numberic format to various formats
- class BlogPost {
- // Class-level variables:
- public $id;
- public $title;
- public $post;
- public $author;
- public $url;
- public $tags;
- public $datePosted;
- function __contruct() {
- // Empty constructor
- $id = null;
- $title = null;
- $post = null;
- $author = null;
- $url = null;
- $tags = array();
- $datePosted = new DateClass();
- }
- // This will construct the object:
- function getPost($inId=null, $inTitle=null, $inPost=null, $inAuthorId=null, $inUrl=null, $inDatePosted=null) {
- // Gets blog id, title and post:
- if (isset($inId)) {
- $this->id = $inId;
- }
- if (isset($inTitle)) {
- $this->title = $inTitle;
- }
- if (isset($inPost)) {
- $this->post = $inPost;
- }
- // Inline SQL stuff:
- $query = mysql_query ('SELECT date_posted FROM blog_posts WHERE id = ' . $inId);
- $row = mysql_fetch_assoc ($query);
- // Explodes date from database to an array, removing the dashes:
- $inDatePosted = explode( '-', $row['date_posted'] );
- // Sets new date and constructs it from the retrieved date from the database:
- $date = new DateClass;
- $date->setDate( $inDatePosted[2], $inDatePosted[1], $inDatePosted[0]-2000 );
- $this->datePosted = $date->fullDate;
- // Sets up author and URL from the database:
- if (isset($inAuthorId)) {
- $query = mysql_query("SELECT first_name, last_name, url FROM people WHERE id = " . $inAuthorId);
- $row = mysql_fetch_assoc($query);
- $this->author = $row['first_name'] . ' ' . $row['last_name'];
- $this->url = '<abbr title="Visit ' . $this->author . '\'s website"> '
- . '<a href="' . $row['url'] . '">' . $row['url'] . '</a></abbr>';
- }
- // Sets up tags:
- $postTags = 'No Tags';
- if( isset( $inId ) ) {
- $query = mysql_query( 'SELECT tags.* FROM blog_post_tags LEFT JOIN (tags)' .
- 'ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = ' . $inId);
- $tagArray = array();
- $tagIDArray = array();
- while($row = mysql_fetch_assoc($query)) {
- array_push($tagArray, $row["name"]);
- array_push($tagIDArray, $row["id"]);
- }
- if( sizeof( $tagArray ) > 0) {
- foreach ($tagArray as $tag) {
- if ($postTags == "No Tags") {
- $postTags = $tag;
- } else {
- $postTags = $postTags . ", " . $tag;
- }
- }
- }
- }
- $this->tags = '<abbr title="Search Google for ' . $postTags . '"> '
- . '<a href="https://www.google.co.uk/search?hl=en&safe=on&q=' . $postTags
- . '" target="_blank">' . $postTags . '</a> </abbr>';
- return $this;
- }
- function __destruct() {
- ;;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement