Home arrow PHP arrow Page 5 - Handling Entries for a Blogger Built with PHP

Defining the displayHeader() and displayFooter() methods - PHP

Are you searching for an accessible guide on how to create a blog application with PHP 5? Then this set of articles might be what you’ve been looking for! Welcome to the second part of the series “Building a Blogger with PHP.” In three parts, this series demonstrates in a few easy steps how to build a classic blog application in PHP 5 by following an object-oriented approach.

TABLE OF CONTENTS:
  1. Handling Entries for a Blogger Built with PHP
  2. Defining the displayBlogs() method
  3. Defining the displayInsertForm() method
  4. Creating the displayUpdateForm() method
  5. Defining the displayHeader() and displayFooter() methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
November 28, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: