Handling Entries for a Blogger Built with PHP - Defining the displayBlogs() method (
Page 2 of 5 )
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:
// display all the blogs
private function displayBlogs(){
$result=$this->mysql->query("SELECT * FROM blogs");
$output='';
while($row=$result->fetchRow()){
$content=nl2br($row['content']);
$output.=<<<EOD
<div class="blog">
<h2>$row[title]</h2>
<h3> Author: $row[author]<h3>
<h3> Posted: $row[date]</h3>
<p>$content</p>
<form action="$_SERVER[PHP_SELF]" method="post">
<input type="submit" value="Edit Blog" name="editblog" />
<input type="submit" value="Delete Blog" name="deleteblog" />
<input type="hidden" value="$row[id]" name="id" />
</form>
</div>
EOD;
}
return $output;
}
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.