logo
Published on Code2Design (http://www.code2design.com)

You Can CMS Without One Two

By maspick
Created Apr 5 2007 - 8:07pm

In my last posting, I demonstrated how to create a header and footer file to be included at the top and bottom of all, or at least many, pages. This makes it easy to modify those parts and have the changes automatically spread across all the pages that share that header and footer file. This is one of the things a CMS simplifies for the user.

But what if part of your header, or footer, file is your navigation system and you want the button or link to change when the page associated is chosen? That means your header, or footer, must change depending on what page it's included on. Actually, that's one of the beautiful things about PHP - since it's commands are processed prior to the display of the page, you can program those changes to happen for you automatically.

The first thing we need to do is determine what page is being displayed at the moment. The following bit of code, placed at the top of your header file so that it's executed first, will get you the page you're on:

<?php
$parts 
explode('/'$_SERVER['PHP_SELF']);
$this $parts[count($parts) - 1];
$pname substr($this0, -4);
?>

What this does is isolate the filename part of the path from the subdirectory(ies) it resides in. The first command creates an array called parts from whatever is between the slashes ('/') of the path. The next determines the filename & extension based upon the index value of the last array element. Finally, we pull off the extension in the third command. The second and third commands could be combined, but there's a reason I keep them separate that I'll explain in a bit.

Okay, we have the name of the page at our disposal, what do we do with it? In our navigation, we can now make decisions about what to do when a certain page is loaded. Here's an example of one navigation element:

<div id="navigation">
<ul>
<li<?php if ($pname == 'guestbook') echo " class=\"there\""?>><?php if ($pname != 'guestbook') { ?><a href="guestbook.php"><?php ?>Guestbook<?php if ($pname != 'guestbook') echo "</a>"?></li>

Here's what's happening in this list item. Before we close the LI tag, we check to see if the page name ($pname) matches the page this item links to. If it does, we add the class named there which will invoke different styling for that item to indicate on the item itself we're on that page. What follows is a determination whether or not we need to make this item a link based upon whether the page is loaded or not.

Looks like a lot of typing, but if you have a means of saving blocks of code that you use frequently, you can save off one of these lines and copy it to your header or footer the number of times to match the number of navigation elements you have. Now you'll have an automatically changing navigation system that'll reflect what page you're on without having to remember to code it on the page itself. And since it's located in one easy-to-edit place, you can modify it as needed when changes happen to your site and it will be reflected in all the pages.

Another feature I usually add to the footer of pages is the last modified date. With information we grabbed in the header and the following code, it will automatically be changed as we update a page.

page last modified <?php echo date("m/j/y h:i"filemtime($this)); ?>

This prints the modified date of the filename ($this), giving the date and time that is stored for that page. Nothing earth-shattering, but a handy thing that some visitors look at to determine how old the information is on a particular page.

Next time, we'll explore some more ways to employ PHP to make your static CMS a little less static seeming. Till then, happy coding! :^{>


Source URL:
http://www.code2design.com/maspick/you_can_cms_without_one_two