Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Tutorial Mix Fresh Tuts Photoshop Cafe Player-Art 

Change Language

Who's online

There are currently 0 users and 3 guests online.

You Can CMS Without One Two

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! :^{>


Submitted by maspick on April 5, 2007 - 8:07pm. |
maspick's blog | printer friendly version

great techniques.

I use all three of these from time to time. Great minds think alike?

Expanding on your navigation tip, I set up an array that contains the navigation, and loop through it when determining the current page and creating the menu. This, also, saves a few bytes.


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati