It's time to unbreak the links at the index.php-page. This is done with not four, but ONE file. If you've followed along in the code noticed that we passed a QUERY-value into the updatePage()-function. If you look at the index.php-file (page 4 in this tutorial) you see that we pass in values like "®i", "&comp", "&cons" and "&memb". These values are applied to the url as a querystring by the updatePage()-function. You can also see that we pass in the value "menu" as the PAGE value. This means that when you, for example, click on the Regions-link you will ask for the page menu.php while passing on the querystring page=regi. Let's create the "menu.php" and see what it does:
The first code chunk we'll create is the switchPage-chunk. This chunk will be used on several pages and in order to make as DRY code as possible we'll put it in a separate file to be imported.
<?php
$page = $_GET["page"];
switch ($page) {
case "memb" : $title = "Members"; $order = "username"; break;
case "comp" : $title = "Companies"; $order = "companyName"; break;
case "regi" : $title = "Regions"; $order = "regionName"; break;
case "cons" : $title = "Consoles"; $order = "consoleName"; break;
}
$pageName = $page;
$page = "&page=".$page;
?>
This is what happens:
First the page (this was submitted in the index.php-file) value of the querystring is stored in the PHP-variable $page
The switch-function test the value to see what page it is you want. (NOTE: If you add a new menu at the Index.php-file you also need to add a corresponding row here!). Two variables will be set. The values these receive depend on what page you wanted. $title will receive the title of the page and $order will store the field name that will be used as default sorting in the LIST-page.
$pageName is a varible that we won't need until page 08. It basically stores the value of $page and keeps it while $page is altered so it can be reused as a querystring again.
The purpose of the menu.php-page is to give us the basic CRUD-options for each table in the database. We want to be able to Create new data into the table,we want to Read the data, we want to be able to Update the data already in the table and finally we want the possibility to Delete data. These are all common menues and goes for all tables in the database. To make the code as DRY as possible that should be done with one single file. And that's the file we're going to create now:
<?php include("php/switchPage.php"); ?>
<h2><?php echo $title; ?></h2>
<a class="menu indent" href="javascript:updatePage('create','2','<?php echo $page; ?>');">Add</a>
<a class="menu right" href="javascript:updatePage('read','0','<?php echo $page; ?>&order=<?php echo $order; ?>');">List</a>
<a class="menu" href="javascript:updatePage('update','2','<?php echo $page; ?>');">Edit</a>
<a class="menu" href="javascript:updatePage('delete','2','<?php echo $page; ?>');">Delete</a>
This file contains plain HTML with highlighted PHP-blocks. At the first row we import the file that we created earlier on this page. This gives us access to all those variables that where set in that file.
Here some new links are created (which are currently broken) and variables created by the PHP-script above is passed in as values in the updatePage()-function.
The next step in the link chain has to do with the database, and that's currently all empty. So before we fix those broken links it's time to work with our database again.
Next Up: Creating our first database table