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 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 do
includes/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.
*/

unset($pages);
$pages[""] = "main.inc";
$pages["home"] = "main.inc"; //setup home as well
$pages["about"] = "aboutus.inc";
//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 . "header.inc";

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

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

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

?>


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

<body>
<h1>Example Site</h1>

includes/footer.inc
<span class="copyright">© 2007 my website. all rights reserved</span>
</body>
</html>

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

includes/aboutus.inc
<h2>About Us</h2>
<p>Here you can find all about us!</p>

includes/error.inc (If you uncomment the error code)
<h2>Error</h2>
<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 )

Testing
To test your page, navigate to these urls: (home should load when trying a bad keyword)
index.php?a=
index.php?a=home
index.php?a=about
index.php?a=badkeyword