Sunday, March 28th, 2004

PHP Link Identifier

I wanted a php script that identified which page in the navigation bar the user was on when the navigation was added using ssi on multiple pages. I found examples in ASP, Cold Fusion, and Server-Side Includes but no PHP. (I found one later that didn’t quite work the way I wanted it to. It uses an array with the names of all of the files and I wanted the page to self-identify so I didn’t have to do upkeep.)

I managed to figure out some code, using the examples as pointers and looking through the functions on the main PHP site. All you have to do is put (or include) the following in the head of all of you pages you’ll include the navigation on.

<?php
//get the file path of the current webpage as a variable
$path = $_SERVER['DOCUMENT_ROOT'];
// the variable $file is set to “index.php”, etc.
$file = basename ($path);
  ?>

Then, format the links in your navigation bar as if…else statments.


<ul>
<?php if($file == "index.html"){print '<li class="on">';}else { print '<li>';}?><a href="index.html" class="on">Home</a></li>

<li><a href=”http://www.someothersite.com” >An External Link</a></li>

<?php if($file == “thisfile.html”){print ‘<li class=”on”>’;}else { print ‘<li>’;}?><a href=”thisfile.html”>This File</a></li>

<?php if($file == “thatfile.html”){print ‘<li class=”on”>’;}else { print ‘<li>’;}?><a href=”thatfile.html”>That File</a></li>

<?php if($file == “theotherfile.html”){print ‘<li class=”on”>’;}else { print ‘<li>’;}?><a href=”theotherfile.html”>The Other File</a></li>
</ul>

Don’t worry about mixing links you want checked and the you don’t, the code around each link is self-contained so it just skips over links that aren’t coded. Basically, it says “If the variable $file is the same as this link’s file-name, write a list-item with a class of “on”, otherwise, just write a list-item.

If the page is in the list, the link to it gets highlighted, if not, it’s skipped.

By Laura @ 8:57 pm in Tutorials

Comments are closed.

map1 map2 map3 map4 map5 map6 map7 map8 map9 map10 map11 map12 map13 map14 map15 map16 map17 map18 map19 map20 map21 map22 map23 map24 map25 map26 map27 map28 map29 map30 map31 map32 map33 map34 map35 map36 map37 map38 map39 map40 map41 map42 map43 map44 map45 map46 map47 map48 map49 map50 map51 map52 map53 map54 map55 map56 map57 map58 map59 map60 map61 map62 map63 map64 map65 map66 map67 map68 map69 map70 map71 map72 map73 map74 map75 map76 map77 map78 map79 map80 map81 map82 map83 map84 map85 map86 map87 map88 map89 map90 map91 map92 map93 map94 map95 map96 map97 map98 map99 map1 map2 map3 map4 map5 map6 map7 map8 map9 map10 map11 map12 map13 map14 map15 map16 map17 map18 map19 map20 map21 map22 map23 map24 map25 map26 map27 map28 map29 map30 map31 map32 map33 map34 map35 map36 map37 map38 map39 map40 map41 map42 map43 map44 map45 map46 map47 map48 map49 map50 map51 map52 map53 map54 map55 map56 map57 map58 map59 map60 map61 map62 map63 map64 map65 map66 map67 map68 map69 map70 map71 map72 map73 map74 map75 map76 map77 map78 map79 map80 map81 map82 map83 map84 map85 map86 map87 map88 map89 map90 map91 map92 map93 map94 map95 map96 map97 map98 map99 map100