PHP
  Home arrow PHP arrow Page 2 - Completing a Blogger with PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Completing a Blogger with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2006-12-05

    Table of Contents:
  • Completing a Blogger with PHP
  • A quick look at the BlogProcessor class
  • Providing the blog processor with database connectivity
  • Implementing client-side data validation on input forms
  • Improving the blogger’s visual appearance

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Completing a Blogger with PHP - A quick look at the BlogProcessor class


    (Page 2 of 5 )

    As I usually do in each of my articles on web development, before I leap forward and start giving the final touches to the blog application, I’d like to show you briefly how the “BlogProcessor” class looked originally. This will refresh your memory before we proceed.

    Here is the full source code that corresponds to the mentioned class, in accordance with the signature defined in the previous article. Please, have a look at it:

    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();

                }

    }

    As you’ll probably remember at this point, the above blog processor class has been provided with all the required methods for inserting, updating and deleting blog entries, in addition to displaying these entries in the browser. However, you’ll notice this class also accepts an object of type “MySQL,” which is very convenient for performing all the database-related operations inside the context of the class.

    Now that you hopefully recalled how the “BlogProcessor” class was defined originally, you’re surely wondering…what’s the next step? Well, obviously I need to show you the respective signature of the “MySQL” class, which is aggregated by the blog processor. In the following section, I’ll be doing exactly that.

    Want to learn how this MySQL wrapping class really looks? Be a bit more patient and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In this final part of the series, the PHP 5-based blogger is completed. This process...
       · Another nice article Alejandro, it hints at an object oriented approach to any kind...
       · Hi Jon,Thank you for the kind comments in my PHP article. I appreciate that, and...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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