PHP
  Home arrow PHP arrow Page 5 - Defining the Core Structure of a PHP Blogger
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

Defining the Core Structure of a PHP Blogger
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2006-11-21


    Table of Contents:
  • Defining the Core Structure of a PHP Blogger
  • Defining the blogger's core structure
  • Defining the insertBlog() method
  • Defining the updateBlog() method
  • Defining the deleteBlog() method

  • 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


    Defining the Core Structure of a PHP Blogger - Defining the deleteBlog() method
    ( Page 5 of 5 )

    As you may guess, removing existing blog entries from the respective "blogs" database table is only a matter of running a DELETE command, wrapped up in a new class method. For this specific case, I decided to name this method "deleteBlog()" and its definition is listed below. Have a look at it:

    // 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 can see, the above "deleteBlog()" method first removes a specified entry from the corresponding "blogs" database table, and then reloads the main web page, in this way updating the display of all pertinent blogs. As I explained in a previous section, you shouldn't be concerned yet with how the "BlogProcessor" class will display the different user panels that I showed you a few lines before, since this topic will be covered over the course of the next article.

    At this stage, I have provided you with a concise explanation of how the blog processor class adds a new entry to the respective database table, in addition to showing the signature of the methods tasked with updating and deleting exiting blogs.

    Thus, this is a good time to list the new definition of the "BlogProcessor" class, after including the mentioned methods. Here is how this class now looks:

    // 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']);

    }
    }

    Okay, above you have the completely updated version of the "BlogProcessor" class. As I mentioned previously, there are still numerous methods that need to be defined, but this will be performed in the following article of the series. In the meantime, study the source code of all the methods shown here and feel free to introduce your own improvements, or if you want to skip over these steps, download this zip file containing the core files of the application (the same link is included at the beginning of this article).

    Wrapping up

    In this first part of the series, I've drawn some basic outlines for how to create a blog application with PHP 5. Due to its versatility, the program can be easily adapted to work with PHP 4, in case you're currently working with that version of the language.

    However, there are many interesting topics to cover yet. Over the next article, I'll define all the remaining methods required by the "BlogProcessor" class to display the complete list of blogs, as well as for rendering the corresponding web forms for inserting and updating database entries. See you there! 



     
     
    >>> 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