Monday, January 08, 2007

Websites are wonderful for giving the user information about your product or service. Sometimes the amount of pages are staggering, and updating many pages takes time. PHP is great for websites that have dynamic content. In this day and age, using multiple HTML files is too much hassle when making a subtle change. In this example, you will setup a website that appears to do everything inside index.php (even though includes are performing the work). This is a good way to navigate your site using only one GET variable. We will be using 4 files in this example, altho it can be expanded upon very easily.

How it all worksWhen the request to index.php is made, PHP will first check the url to see if the variable "a" has been set (e.g. ?a=home). It will check this value against the keys of the $pages array, if the key is found inside the array, the value of the array (the filename) is passed to the include function and loaded. In the case that the variable is non-existing or a 'bad value' the default page ($default_page) will be loaded. The header.inc and footer.inc files will automatically be inserted before and after the content page respectively.

Required Files

These files are included, not linked to. Your users will never know these files exist, unless you have an error message dump it out somewhere.(error.inc and style.css are not required for this example)

index.php - this is the daddy script that determines what to doincludes/header.inc - this file will contain everything from the doctype to the end of the header/nav system.includes/footer.inc - this file will contain everything from copyright to the final </html> tag.includes/main.inc - this file will contain the middle part that has all the content.includes/aboutus.inc - this file will contain the middle part dealing with the about us page.includes/error.inc- (optional) this file will contain the middle part dealing with the about us page.style.css - (optional) this is where your styles will go, should you seperate them from header.inc

Lets look at the files:

index.php<?php

/*What this does:This script will load a header, content, and a footer for output to the browser.The content file is loaded based on the value of $_GET["a"].This script will double check to ensure that the keyword is valid, otherwise it uses a default.No complicated if or switch statements are used, this is where the $pages array comes in.

Breakdown:$cur_page will be populated from $_GET["a"] (the keyword), eg: index.php?a=home or index.php?a=$pages contains an array of 'keys' (for ?a=) and 'filenames' (in includes/) to valid pages that can be accessed.If the keyword doesn't exist, $default_page will be loaded instead. (this can be modded to use an extra error.inc file)The include file is loaded.*/