PHP code to create an online tour of website pages

I felt like doing some programming today.  So, I decided to add an online tour to one of my websites, www.XwayXway.com, which is a geo-domain about Stanley Park in Vancouver, B.C., Canada.  I wanted to have links on every .html page that the user can click on to view all the pages of the web site in a particular order.

I decided to do this task using PHP so I didn’t have to duplicate code in every .html file and it would be easier to maintain having one file with the order of all the .html files.  Also,  I had already enabled PHP for my websites on my web host so it was available to use.

First, I created a PHP file called: PHP_StanleyParkTourBottom.php

First, to make it generic, I needed to determine what .html page the user was on:

//get the webpage name, e.g. index.html

$url = $_SERVER[‘REQUEST_URI’];
//strip off the first backslash
$url = trim(str_replace(“/”,””,$url));
//make it uppercase
$url = strtoupper($url);
//echo $url;

This involved a call to the server.  I also stripped off the leading “/” and made it uppercase.

Next, I needed to handle the main webpage to start the tuturial:

//main page may be index.html or blank
if ($url == “INDEX.HTML” or $url == “”)
{
//create the table for the main web page
echo “<!–NAVIGATION–>”;
echo “<p id=\”StanleyParkTourBottomHeading\”>”;
echo “Stanley Park Tour”;
echo “</p>”;
echo “<table id=\”tblStanleyParkTourBottom\”>”;
echo “<tr>”;
echo “<td class=\”tdStanleyParkTourBottom\”><a href=\”XwayXway_Stanley_Park_Tours.html\”>Click Here to Start Free Online Tour of Stanley Park</a></td>”;
echo “</tr>”;
echo “</table>”;
echo “<!–NAVIGATION-END–>”;
}

I decided to use a table.  What you don’t see here is the CSS to make a thick border around the “Click here…” part to make it stand out.  I used “echo” statements to produce the HTML code.  It was a little tricky getting the double-quotes correct.  Remember you have to use \” to do a literal double-quote.

Then, I needed to handle the other .html webpages:

else
{
//first get the previous and next page urls
switch ($url)
{
case “XWAYXWAY_STANLEY_PARK_TOURS.HTML”:
$urlPrevious = “index.html”;
$urlNext = “XwayXway_Stanley_Park_Horse_Drawn_Tours.html”;
break;
case “XWAYXWAY_STANLEY_PARK_HORSE_DRAWN_TOURS.HTML”:
$urlPrevious = “XwayXway_Stanley_Park_Tours.html”;
$urlNext = “XwayXway_Stanley_Park_Aerodynamic_Forms_In_Space_Art.html”;
break;

…   about 100 more cases …

default:
$urlPrevious = “”;
$urlNext = “”;
}

I decided to use a switch statement within the PHP.  I could have created a MySql database and table, but decided that would be overkill.  The switch statement contained the html pages that are on the website.  For each one, the previous and next html pages were defined.  There were some pages that I didn’t want in the tour.  These pages will default to no previous and next page.

Then, I needed to create the tables with the specific previous and next values:

if (!($urlPrevious == “” and $urlNext == “”))
{
//create the table for the current web page
echo “<!–NAVIGATION–>”;
echo “<p id=\”StanleyParkTourBottomHeading\”>”;
echo “Stanley Park Tour”;
echo “</p>”;
echo “<table id=\”tblStanleyParkTourBottom\”>”;
echo “<tr>”;
echo “<td class=\”tdStanleyParkTourBottom\”><a href=\”” . $urlPrevious . “\”>Previous</a></td>”;
echo “<td class=\”tdStanleyParkTourBottom\”><a href=\”” . $urlNext . “\”>Next</a></td>”;
echo “</tr>”;
echo “</table>”;
echo “<!–NAVIGATION-END–>”;
}

Later, I decided to add in a section for the pages that I didn’t include in the tour.  They would just have a link to start the tour similar to the main web page.
else
{
//create the table for the tour start for pages not in the tour
echo “<!–NAVIGATION–>”;
echo “<p id=\”StanleyParkTourBottomHeading\”>”;
echo “Stanley Park Tour”;
echo “</p>”;
echo “<table id=\”tblStanleyParkTourBottom\”>”;
echo “<tr>”;
echo “<td class=\”tdStanleyParkTourBottom\”><a href=\”XwayXway_Stanley_Park_Tours.html\”>Click Here to Start Free Online Tour of Stanley Park</a></td>”;
echo “</tr>”;
echo “</table>”;
echo “<!–NAVIGATION-END–>”;
}

Then, I needed to add the following PHP include statement to each of the 100+ html files on my website:

<?php include(‘PHP_StanleyParkTourBottom.php’); ?>

So, that completes  It was a little tedious adding in all the cases and adding the 1 line to over 100 files, but I am happy with the result.  I’m hoping that the tour will increase the page views and clicks on ads on www.XwayXway.com

Leave a Reply