Here's what I'm talking about - the menu shows what page you're on - and here's a great way to do this with PHP.
First, save your menu in an external file called 'menu.htm'.
Here's mine:
HTML
<ul id="menu">
<li><a href="index.php">Home</a></li>
<li><a href="one.php">Page One</a></li>
<li><a href="two.php">Page Two</a></li>
<li><a href="three.php">Page Three</a></li>
<li><a href="four.php">Page Four</a></li>
<li><a href="five.php">Page Five</a></li>
</ul>
Then use this preg_replace code to take your menu, find the link to the current page, remove the 'a' tags and give the 'li' tag a class ('current') and print your menu on the page.
PHP
<?php
$menu=file_get_contents("menu.htm");
$base=basename($_SERVER['PHP_SELF']);
$menu=preg_replace("|<li><a href=\"".$base."\">(.*)</a></li>|U", "<li class=\"current\">$1</li>", $menu);
echo $menu;
?>
It's that simple.
(This can also be done without php by using the cascade).
Yep, that little PHP script certainly does the trick. Nice example.
Myself though, I prefer to avoid scripting if I can, so I use a CSS only method. I believe I first saw it here: trawna.com.
How it's used:
1) On every content page, a page-specific class is set to the body tag
2) In the CSS, the style is changed for the menu link that corresponds to the body class of the current page
Example:
Current page's body tag: <body class="body-contact">
ID for the menu list on all pages: "Menu"
ID of the current page's link in the menu: "li-contact"
CSS to display current page:
.body-forum #Menu #li-forum,
.body-contact #Menu #li-contact,
.body-faq #Menu #li-faq
{
color:#a00;
background-color:transparent;
font-weight:bold;
}
Since the class of the current page's body is "body-contact", the link that gets highlighted in the menu is the one with the ID "li-contact".
Oops, sorry - I didn't see your last line there until now, but now I see that you have already explained "the cascade" technique.
I thought I might add: the only reason why I said I preferred the cascading technique was that I am more used to having static HTML pages than PHP pages. However, if you're already using PHP, I would definitely agree that using PHP for this code is a smarter choice.
first of all i must say thank you for such a wonderful article. I'm a n00b with Ajax, JS and this article has helped me get started. I had a question, is it possible to indicate the current page i.e. when a user is on that page indicate current page by changing its color. I know you have the php/css tutorial but is it possible for you to add this an Ajax/JS tutorial - Thanks.
I'm really a newbie here, so sorry if this is a dumb question..
I'm wondering how you edit the text that's displayed on the separate php pages. (e.g. Pageonethisispageone) Is it just part of the file itself like here? Or another separate file? I'm a little confused.
thanks,
luna
I have used php to identify the current page on my main menu.
I'm guessing there is no way to use the php to do it with a submenu link that only goes to a <div id=#submenu1"> on the page without reloading?
I'm trying to figure out how to mark a submenu item current if there is no javascript.
do u think u could maybe explain a little more what all those symbols and codings do in the PHP file...
im sh00ter555 from htmlForums.