Monday, January 08, 2007

PHP: creating a simple GET navigation system

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 works
When 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 and 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.
( and style.css are not required for this example)

index.php - this is the daddy script that determines what to do
includes/ - this file will contain everything from the doctype to the end of the header/nav system.
includes/ - this file will contain everything from copyright to the final </html> tag.
includes/ - this file will contain the middle part that has all the content.
includes/ - this file will contain the middle part dealing with the about us page.
includes/ - (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

Lets look at the files:


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.

$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 file)
The include file is loaded.

$pages[""] = "";
$pages["home"] = ""; //setup home as well
$pages["about"] = "";
//add more pages here!

$cur_page = $_GET["a"];
$default_page = ""; //this needs to be a valid key from $pages
$inc_path = "includes/";

//lets include our header
include $inc_path . "";

if ($pages[$cur_page]){
//lets include the correct file
include $inc_path . $pages[$cur_page];
//the keywords didn't match anything so lets load $default_page
include $inc_path . $pages[$default_page];

//OR you could have another file loaded
//include $inc_path . ""; //include a custom error handler

//lets include our footer
include $inc_path . "";


<title>This is an example site</title>
<style type="text/css">
.copyright {font-size: 10px; text-align: center}

<h1>Example Site</h1>

<span class="copyright">© 2007 my website. all rights reserved</span>

<h2>Welcome to Main!</h2>
<p>This is an example of what could go on the main page!</p>

<h2>About Us</h2>
<p>Here you can find all about us!</p>

includes/ (If you uncomment the error code)
<p>There was an error found!</p>

Thats it! Copy the files to your php webserver, be sure the paths are correct and the files are in the proper places. Also, links should start with: "index.php?a=KEYWORD&more=vars"... etc etc (or just:
?a=KEYWORD&more=vars if its' your index.php file :P )

To test your page, navigate to these urls: (home should load when trying a bad keyword)