Determine section of the website for navigation
Posted: Sat Apr 28, 2007 2:15 am
It's often useful to show a user in which section of the website he is, by highlighting the current section in the navigation bar. For a website I build I wrote the snippet below. I use it to determine the (main) section we are in.
This website uses clean URLs, so they always look like /services/, /services/webdesign/, /contact/, etc. Each main section can have more subpages. Therefore I couldn't use the page_ID.
Then in the CSS I do something like:
But my main concern is the PHP. So any tips, critique, security issues, code beautifers?
This website uses clean URLs, so they always look like /services/, /services/webdesign/, /contact/, etc. Each main section can have more subpages. Therefore I couldn't use the page_ID.
Code: Select all
<?php
// only allow specific set of sections
$allowed_sections = array('home','services','portfolio', 'weblog', 'about', 'contact');
// the URLs look like /portfolio/ , /contact/, etc
$request_uri = $_SERVER["REQUEST_URI"];
$parts = explode(DIRECTORY_SEPARATOR, $request_uri);
if(in_array($parts[1], $allowed_sections)) {
$section = $parts[1];
} else {
$section = 'home';
}
// Then, in the template for the header section of all pages:
echo '<body id="' . $page_ID . '" class="' . $section . '" >';
?>Code: Select all
#navbar {background:transparent url(images/navbar.gif) 0 0 no-repeat;}
.services #navbar {background-position: 0px -28px;}
//etc