HomePHP Page 2 - Handling Entries for a Blogger Built with PHP
Defining the displayBlogs() method - PHP
Are you searching for an accessible guide on how to create a blog application with PHP 5? Then this set of articles might be what you’ve been looking for! Welcome to the second part of the series “Building a Blogger with PHP.” In three parts, this series demonstrates in a few easy steps how to build a classic blog application in PHP 5 by following an object-oriented approach.
Prior to extending the existing functionality of the "BlogProcessor" class that you learned in the first article, I'd like to show its original signature so you will remember how it looked. Having said that, here is the definition of this class as it was initially created:
// 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']); } }
As you'll recall, the above class was only capable of performing some limited tasks, such as adding new entries to the "blogs" database table, and updating/removing existing ones. However, as I said in the beginning, the referenced class must also be capable of doing many more things, like displaying the complete list of entries stored on the corresponding database.
In response to these requirements, below I appended a new method to the class which shows the entire set of blog entries, in conjunction with a pair of buttons for updating and deleting each blog in question. Thus, have a look at this brand new method, please:
If you examine the source code that corresponds to the above method, you'll agree that its definition is really simple. In short, all that this method does is fetch all the entries stored on the respective database table and display them on the browser. Also, you should notice the inclusion of the two control buttons that I mentioned before, which come in useful for updating and deleting a particular entry.
With reference to these controls, you'll realize that each time one of them is clicked, the corresponding form that wraps them is submitted to itself. Certainly, this condition can be easily checked inside the blog processor class to trigger the respective processes for removing or eventually updating blog entries.
In the next few lines I'll add another handy method to the blog processor, which will be responsible for showing the corresponding online form where users can insert a new blog entry into the database table for further display.
To see how this new method will be defined, click on the link that appears below and keep reading.