PHP
  Home arrow PHP arrow Page 5 - 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 displayHeader() and displayFooter() methods
    ( Page 5 of 5 )

    Having provided the "BlogProcessor" class with the ability to display the web forms required for inserting and updating blog entries, the only step that remains still undefined is assembling the different sections that compose the main web page of the blogger in question.

    In order to address the issues that I mentioned above, I'm going to define two additional methods. The the first one obviously will take care of generating the header of the main page, while the second one will be aimed at rendering the footer.

    Now that you know what tasks will be performed by these two new methods, please have a look at their respective signatures. Here they are:

    // display page header
                private function displayHeader(){
    $output=<<<EOD
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Blogger System</title>
    <link href="styles.css" rel="stylesheet" type="text/css"
    media="screen" />
    <script language="javascript" src="valfunctions.js"></script>
    </head>
    <body>
    <h1>PHP-BASED BLOG SYSTEM</h1>
    EOD;
                return $output;
    }
    // display page footer
    private function displayFooter(){
                return '</body></html>';
    }

    As you can see above, the "displayHeader()" and "displayFooter()" methods are simple wrappers for the respective header and footer sections that comprise the primary web page of the blog application. Also, you should notice that I decided to include a CSS file along with the header, which logically will improve the overall look and feel of the program.

    In addition, the "displayHeader()" method includes a JavaScript file called "valfunctions.js" that will take care of performing a basic client-side validation on the data entered in the respective insertion and update web forms. However, both CSS and JavaScript files that I mentioned previously will be shown in the last article of this series, therefore at least for the moment, keep them away of your mind.

    Okay, now that you learned the signature for this pair of new methods, I'll create two new useful ones, in this case called "displayMainPage()" and "displayUpdatePage()" respectively. As their names suggest, they will be tasked with rendering the primary web page of the blogger and the update web document.

    That said, here are the corresponding definitions for these private methods:

    // display main page
    private function displayMainPage(){
                return $this->displayHeader().$this->displayBlogs
    ().$this->displayInsertForm().$this->displayFooter();
    }
    // display edit page
    private function displayEditPage(){
                return $this->displayHeader().$this-
    >displayUpdateForm().$this->displayFooter();
    }

    Wasn't that simple? The two additional methods that I just created above are the last ones required to complete the blogger. Of course, I'm quite sure that you want to see the full source code that corresponds to the blog application. Below you can see how the complete "BlogProcessor" class now looks:

    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']){
                                       // display edit page
                                       return $this->displayEditPage();
                            }
                            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();       
                                       }
                            }
                            // display main page
                            return $this->displayMainPage();
                }
                // 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']);
                }
                // 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;
                }
                // display insertion form
                private function displayInsertForm(){
    $output=<<<EOD
    <div class="dataform">
    <h2>Insert New Blog</h2>
    <form action="$_SERVER[PHP_SELF]" method="post" id="insertform">
    <p>Title:</p><p><input type="text" name="title" class="datafield" /></p>
    <p>Author:</p><p><input type="text" name="author" class="datafield" /></p>
    <p>Type your content below</p><p><textarea
    name="content"></textarea></p>
    <p><input type="submit" name="insertblog" value="Insert Blog" /></p>
    </form>
    </div>
    EOD;
                            return $output;   
                }
                // display update form
                private function displayUpdateForm(){
                            $id=$this->blogData['id'];
                            $result=$this->mysql->query("SELECT * FROM blogs WHERE
    id='$id'");
                            if($result->countRows()>0){
                                       $row=$result->fetchRow();
    $output=<<<EOD
    <div class="dataform">
    <h2>Update Blog</h2>
    <form action="$_SERVER[PHP_SELF]" method="post" id="updateform">
    <p>Title:</p><p><input type="text" name="title" value="$row[title]"
    class="datafield" /></p>
    <p>Author:</p><p><input type="text" name="author" value="$row[author]"
    class="datafield" /></p>
    <p>Update your content below</p><p><textarea name="content">$row[content]</textarea></p>
    <p><input type="submit" name="updateblog" value="Update Blog" /></p>
    <input type="hidden" value="$id" name="id" />
    </form>
    </div>
    EOD;
                                       return $output;
                            }
                }
                // display page header
                private function displayHeader(){
    $output=<<<EOD
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Blogger System</title>
    <link href="styles.css" rel="stylesheet" type="text/css" media="screen" />
    <script language="javascript" src="valfunctions.js"></script>
    </head>
    <body>
    <h1>PHP-BASED BLOG SYSTEM</h1>
    EOD;
                            return $output;
                }
                // display page footer
                private function displayFooter(){
                            return '</body></html>';
                }
                // display main page
                private function displayMainPage(){
                            return $this->displayHeader().$this->displayBlogs().$this-
    >displayInsertForm().$this->displayFooter();
                }
                // display edit page
                private function displayEditPage(){
                            return $this->displayHeader().$this->displayUpdateForm().$this-
    >displayFooter();
                }
    }

    Now, after seeing the full source code of the blog application, you'll agree with me that every piece of code makes much more sense. As I say in every article, feel free to tweak the code of each method shown here and introduce your own modifications. It's really fun!

    Wrapping up

    In this second part of the series, I defined all remaining methods that belong to the blog processor, in order to turn it into an almost fully-functional class. Why did I say "almost"? Well, there's still a couple of things that need to be done, such as defining the MySQL wrapping class aggregated by the blog processor, in addition to defining the CSS and JavaScript files that I mentioned a few lines before.

    However, as you'll probably imagine, all these issues will be covered in the last part, so you don't have any excuses to miss it!



     
     
    >>> 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 6 Hosted by Hostway
    Stay green...Green IT