HomePHP Page 5 - Defining the Core Structure of a PHP Blogger
Defining the deleteBlog() method - PHP
In the first part of this three-part series, we'll examine the basic outlines of creating a blog application with PHP 5. If you're currently working with PHP 4, this program can be adapted to that version of the language.
As you may guess, removing existing blog entries from the respective "blogs" database table is only a matter of running a DELETE command, wrapped up in a new class method. For this specific case, I decided to name this method "deleteBlog()" and its definition is listed below. Have a look at it:
// delete blog private function deleteBlog(){ $id=$this->blogData['id']; $title=$this->blogData['title']; $author=$this->blogData['author']; $content=$this->blogData['content']; $this->mysql->query("DELETE FROM blogs WHERE id='$id'"); header('Location:'.$_SERVER['PHP_SELF']); }
As you can see, the above "deleteBlog()" method first removes a specified entry from the corresponding "blogs" database table, and then reloads the main web page, in this way updating the display of all pertinent blogs. As I explained in a previous section, you shouldn't be concerned yet with how the "BlogProcessor" class will display the different user panels that I showed you a few lines before, since this topic will be covered over the course of the next article.
At this stage, I have provided you with a concise explanation of how the blog processor class adds a new entry to the respective database table, in addition to showing the signature of the methods tasked with updating and deleting exiting blogs.
Thus, this is a good time to list the new definition of the "BlogProcessor" class, after including the mentioned methods. Here is how this class now looks:
// define 'BlogProcessor' class class BlogProcessor{ private $mysql; private $blogData; public function __construct(MySQL $mysql){ $this->mysql=$mysql; $this->blogData=$_POST; } // display blog system public function displayBlogger(){ // edit blog if($this->blogData['editblog']){ // code for displaying edit page goes here } else{ // insert new blog if($this->blogData['insertblog']){ $this->insertBlog(); } // update blog elseif($this->blogData['updateblog']){ $this->updateBlog(); } // delete blog elseif($this->blogData['deleteblog']){ $this->deleteBlog(); } } // code for displaying main page goes here } // insert new blog private function insertBlog(){ $title=$this->blogData['title']; $author=$this->blogData['author']; $content=$this->blogData['content']; $this->mysql->query("INSERT INTO blogs (id,author,title,content,date) VALUES (NULL,'$author','$title','$content',TIMESTAMP (10))"); header('Location:'.$_SERVER['PHP_SELF']);
} // update blog private function updateBlog(){ $id=$this->blogData['id']; $title=$this->blogData['title']; $author=$this->blogData['author']; $content=$this->blogData['content']; $this->mysql->query("UPDATE blogs SET title='$title',author='$author',content='$content',date=TIMESTAMP(10) WHERE id='$id'"); header('Location:'.$_SERVER['PHP_SELF']);
} // delete blog private function deleteBlog(){ $id=$this->blogData['id']; $title=$this->blogData['title']; $author=$this->blogData['author']; $content=$this->blogData['content']; $this->mysql->query("DELETE FROM blogs WHERE id='$id'"); header('Location:'.$_SERVER['PHP_SELF']);
} }
Okay, above you have the completely updated version of the "BlogProcessor" class. As I mentioned previously, there are still numerous methods that need to be defined, but this will be performed in the following article of the series. In the meantime, study the source code of all the methods shown here and feel free to introduce your own improvements, or if you want to skip over these steps, download this zip file containing the core files of the application (the same link is included at the beginning of this article).
Wrapping up
In this first part of the series, I've drawn some basic outlines for how to create a blog application with PHP 5. Due to its versatility, the program can be easily adapted to work with PHP 4, in case you're currently working with that version of the language.
However, there are many interesting topics to cover yet. Over the next article, I'll define all the remaining methods required by the "BlogProcessor" class to display the complete list of blogs, as well as for rendering the corresponding web forms for inserting and updating database entries. See you there!