I don’t consider myself a web programmer, but I have occasionally fiddled with the odd perl script, php, or asp code snippets. I had a situation yesterday that caused me to want to come up with a php script that would switch tab graphics on a web site, based on the URL of the page.
I’m building the site with Dreamweaver, and my client is going to use Contribute to maintain it. So, I have a page template for the majority of the pages in the site. The navigation is a set of horizontal tabs, and if you are in a certain section of the site, the navigation tabs should indicate that you are in that section.
Unfortunately, this might have meant creating a separate template file for each section of the web site. I thought about this for a few moments, visualizing the nightmare of updates this might create, and decided to try mashing together a script that would solve my problems.
I started by looking through the URL parser in a file I use on my site to create breadcrumb navigation based on the directory. Once I had that piece nailed down, I simply assigned a variable with the part of the URL in question, and then did a series of if else statements to determine which navigation tab html code to include.
So, the pieces:
- navswitcher.php — this determines which html navigation file to include based on the part of the URL immediately following the domain name
- a separate html file containing just the code needed for the navigation tabs; one file per section
- the php include line to add to the Dreamweaver template file where the navigation is supposed to be
Here is the navswitcher code:
// retrieve and parse the web address after the domain name $str = $PHP_SELF; ereg("^(.+)/.+\\..+$", $str, $part); $str = $part[1]; $str = substr($str, 1); // for the first example, the URL might be http://domain.com/client/joe.php // this script grabs the "client" part and makes it the value of $str // set directory name variables $navfiles = "/path/to/html/nav/files"; $client = "client"; $services = "services"; $portfolio = "portfolio"; // set up cases where $str will match certain values and output different files if ($str == $client) include("$navfiles/main_client.txt"); elseif ($str == $services) include("$navfiles/main_services.txt"); elseif ($str == $portfolio) include("$navfiles/main_portfolio.txt"); // if none of those, then it must need the default home navigation else include("$navfiles/main_home.txt");
That code should go into its own file which would then be included in the Dreamweaver template (or from whichever file you need it in).
Doing it this way enabled the site to work from a single Dreamweaver template file, instead of one for each section of the site.
This solution also separates the navigation html itself from the scripting that puts it in place. This means that my client will be able to edit those individual files when they need changing, rather than have to mess around with all the PHP code or Dreamweaver templates (which would require that he own Dreamweaver).