Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Here is the main class for the OO blog: this handles all of the logic
- * from opening and closing the connection to the database to retrieving
- * the posts from it.
- * Please see:
- * net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
- * for further information, or find me on the devshed.com forums if you have any
- * questions.
- * Put in a folder libraries and call this include.php - or set your own paths and
- * names that make more sense to you.
- *
- * @Author: Shaun B
- * @Date: 2012-10-24
- * @Todo: This is currently doing everything that I want it to, however at some
- * point, I will need to implement getting a single blog from a user,
- * edit a users blog or just more logic to make it a more complete
- * website.
- **/
- include 'blogpost.php';
- class Blogs {
- public $server;
- public $dbUser;
- public $password;
- public $database;
- public $connection;
- public $postArray;
- function __construct(){
- $server = null;
- $dbUser = null;
- $password = null;
- $database = null;
- $connection = null;
- $postArray = array();
- }
- // Connects to the database:
- function connect( $serv = null, $usr = null, $pwd = null, $db = null){
- if ( !$serv || !$usr || !$db ) {
- echo 'No database connection established.';
- return null;
- } else {
- $this->server = $serv;
- $this->dbUser = $usr;
- $this->password = $pwd;
- $this->database = $db;
- $this->connection = mysql_connect( $this->server, $this->dbUser, $this->password, $this->database );
- mysql_select_db( $this->database, $this->connection );
- }
- }
- // This will retrieve all of the blog posts from a database - it uses
- // inline SQL:
- function GetBlogPosts($inId=null, $inTagId =null) {
- if (!empty($inId)) {
- $query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC");
- } else if (!empty($inTagId)) {
- $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");
- } else {
- $query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
- }
- $this->postArray = array();
- while ($row = mysql_fetch_assoc($query)) {
- $myPost = new BlogPost();
- $myPost->getPost($row['id'], $row['title'], $row['post'], $row['author_id'], $row['date_posted']);
- array_push($this->postArray, $myPost);
- }
- return $this->postArray;
- }
- // Closes database connection when the object is obliterated:
- function __destruct() {
- if($this->connection) {
- mysql_close($this->connection);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement