Web Resources

DataExpressions inc

Website Development Resources

Bookmark our site
Google
 

PHP - Practical Examples

Standard Headers and Footers

Synopsis

Creating a standard header and footer in a content rich web site is absolutely essential. It is also the epitome of demonstrating code (page) reuse in a practical and time saving way. 

In this example I am going to show you how to create a standard header and footer page and then how to embed these pages into your content page. At the end you can download a zip file which will contain everything you will need to play with

Creating the Header

First we create the header page our_simple_header.php

<?php

echo
'
<table border="0" cellpadding="10" cellspacing="6" width="100%">
<tr>
<td width="100%">
<h1 align="center">Our Simple Header</h1>
</td>
</tr>
<tr>
<td width="100%">
<hr>
</td>
</tr>
</table>
'

?>

As you can see it is standard html code except for the <?php    ?> wrapper and the echo' ' wrapper that surrounds the html code. We also need to create the doctype reference: doctype.php

<?php 
echo '<!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" xml:lang="en" lang="en">'
?>

Creating the Footer

Next we create the Footer page our_simple_footer.php

<?php

echo
'
<table border="0" cellpadding="10" cellspacing="6" width="100%">
<tr>
<td width="100%">
<hr>
</td>
</tr>
<tr>
<td width="100%">
<p align="center">Our Simple Footer </td>
</tr>
</table>
'
?>

Creating a Reusable Template

Now we want to create a template that contains our header and footer, and basic html code. We will reuse this template to create our content pages. As you can see from the code below we include our other Php files using the include( ) function. I have named the template simpleTemplate.php

<?php
include('doctype.php');
?>

<html>

<head>
<title>Template</title>
</head>


<body>

<?php
include('our_simple_header.php');
?>

<!-- Content goes here -->


<!-- End Content -->

<?php
include('our_simple_footer.php');
?>

</body>

</html>

The output in the browser looks like...

We can reuse this template to create different pages. Having a standard header and footer saves us a great deal of effort. Another key advantage is that anytime we change the header or footer we do not have to worry about where we are using them. The change is automatically picked up by every page that includes these. Below is an example of using our page

 While this may seem like an awful lot of work, it really is not. Imagine if you create your header and footer in each and every page. You end up with 50 pages. Sure you can also create a template that only consists of one file. But if you need to make a change you would need to make it to 50 files. Too boot, how are you going to keep track of all the files? It is much easier to use this technique and do the work up front.

Click here to get the files in the creating standard Php headers and footers.








Copyright 2006-2007 by Data Expressions inc --- All Rights Reserved

Links