PHP
  Home arrow PHP arrow Page 2 - Handling Entries for a Blogger Built with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Handling Entries for a Blogger Built with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2006-11-28


    Table of Contents:
  • Handling Entries for a Blogger Built with PHP
  • Defining the displayBlogs() method
  • Defining the displayInsertForm() method
  • Creating the displayUpdateForm() method
  • Defining the displayHeader() and displayFooter() methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT