Advertisement
Shaun_B

This is the Blogs class which gets the blogs from the db

Oct 24th, 2012
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Here is the main class for the OO blog: this handles all of the logic
  4.  * from opening and closing the connection to the database to retrieving
  5.  * the posts from it.
  6.  * Please see:
  7.  * net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  8.  * for further information, or find me on the devshed.com forums if you have any
  9.  * questions.
  10.  * Put in a folder libraries and call this include.php - or set your own paths and
  11.  * names that make more sense to you.
  12.  *
  13.  * @Author: Shaun B
  14.  * @Date:   2012-10-24
  15.  * @Todo:   This is currently doing everything that I want it to, however at some
  16.  *      point, I will need to implement getting a single blog from a user,
  17.  *      edit a users blog or just more logic to make it a more complete
  18.  *      website.
  19.  **/
  20. include 'blogpost.php';
  21. class Blogs {
  22.     public $server;
  23.     public $dbUser;
  24.     public $password;
  25.     public $database;
  26.     public $connection;
  27.     public $postArray;
  28.     function __construct(){
  29.         $server = null;
  30.         $dbUser = null;
  31.         $password = null;
  32.         $database = null;
  33.         $connection = null;
  34.         $postArray = array();
  35.     }
  36.     // Connects to the database:
  37.     function connect( $serv = null, $usr = null, $pwd = null, $db = null){
  38.         if ( !$serv || !$usr || !$db ) {
  39.             echo 'No database connection established.';
  40.             return null;
  41.         } else {
  42.             $this->server = $serv;
  43.             $this->dbUser = $usr;
  44.             $this->password = $pwd;
  45.             $this->database = $db;
  46.             $this->connection = mysql_connect( $this->server, $this->dbUser, $this->password, $this->database );
  47.             mysql_select_db( $this->database, $this->connection );
  48.         }
  49.     }
  50.     // This will retrieve all of the blog posts from a database - it uses
  51.     // inline SQL:
  52.     function GetBlogPosts($inId=null, $inTagId =null) {
  53.         if (!empty($inId)) {
  54.             $query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC");
  55.         } else if (!empty($inTagId)) {
  56.             $query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
  57.         } else {
  58.             $query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
  59.         }
  60.         $this->postArray = array();
  61.         while ($row = mysql_fetch_assoc($query)) {
  62.             $myPost = new BlogPost();
  63.             $myPost->getPost($row['id'], $row['title'], $row['post'], $row['author_id'], $row['date_posted']);
  64.             array_push($this->postArray, $myPost);
  65.         }
  66.         return $this->postArray;
  67.     }
  68.     // Closes database connection when the object is obliterated:
  69.     function __destruct() {
  70.         if($this->connection) {
  71.             mysql_close($this->connection);
  72.         }
  73.     }
  74. }
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement