Home arrow PHP arrow Page 2 - Completing a Blogger with PHP

A quick look at the BlogProcessor class - PHP

If you’re looking for a comprehensive series on how to create an expansible blog application with PHP 5, then this group of tutorials might be what you’ve been expecting to find. Welcome to the concluding installment of the series "Building a blogger with PHP." Made up of three friendly articles, this series provides you with all the information you need to start building a blog system with PHP in a few easy steps.

TABLE OF CONTENTS:
  1. Completing a Blogger with PHP
  2. A quick look at the BlogProcessor class
  3. Providing the blog processor with database connectivity
  4. Implementing client-side data validation on input forms
  5. Improving the blogger’s visual appearance
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
December 05, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: