views:

37

answers:

1

I have all the headers and the top navigation included in the page with PHP

I want to assign a class to the <li class="current">

so i have a list of <li>'s and the current page should get a class of "current"

and the body should get a class of the current page

for http://www.mysite.com/home = the body should be <body class"home"> and so on

Thanks

+1  A: 

You might use something like:

<?
    $parts = explode("/", strtolower(preg_replace("/[^\\/\w]/", "", $_SERVER["REQUEST_URI"])));
?>

[...]

<body class="<? echo $parts[0] ?>">

You can similarily compare the $_SERVER["REQUEST_URI"] to the link of each of your navigation items. Note that the $_SERVER["REQUEST_URI"] can be spoofed by the client, so make sure you kick out anything strange before using it or displaying it back to the page.

Steffen Müller
Thanks, I didn't understand the second part `$_SERVER["REQUEST_URI"]`
adardesign
$_SERVER["REQUEST_URI"] contains the part of the actual url behind your domain name. So, the request uri should be the same as the url the current navigation item links to. if they are equal, assign class "current".
Steffen Müller
thanks, can you please write it out (the same as you did with the parts) thanks much
adardesign
Don't get me wrong, but coding this part is absolutely trivial grunt work every php developer should be able to perform. The idea ofa sking questions here is to understand and to learn, not to find somebody else do the work.
Steffen Müller