Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Handling Entries for a Blogger Built with PHP
  2. Defining the displayBlogs() method
  3. Defining the displayInsertForm() method
  4. Creating the displayUpdateForm() method
  5. Defining the displayHeader() and displayFooter() methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
November 28, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: