Make menu selection active after url rewriting
Most people love URL rewriting with .htaccess, but soon after they manage to work it out, other problems appear. Apart from the "My images aren't displaying!" and "My CSS files aren't included anymore!" problems, which are easily solved by adding a "/" in front of the path and using the full path, other problems appear like how do you know what script you're running so you know which menu option to highlight? I ussually use a custom "pages" class that provides all the functionalities I need, but for the menu I use a single function:
CODE:
function is_htaccess_page($script, $return) {
$bits = explode('/', $this->script());
$bits_no = count($bits);
$page = $bits[$bits_no-1];
if ($page == $script) {
return $return;
}
else {
return false;
}
} Pass the script that you're expecting and a value that you want to have returned in case that's the current script. Like this, you can define a CSS class (say ".current") for your active menu selections, and use the function like this:
CODE:
<a href="/home" class="<?php echo is_htaccess_page('home.php', 'current'); ?>">Home</a> Of course, your links will differ depending on whatever htaccess rules you use, but the main idea is the same. It's the simplest way to keep track of your pages in my opinion and flexible enough to fit any needs.
The code in this article is distributed under the MIT license ![]()
You can visit the author's website at blog.hazardousgaming.info
There are 2 similar articles, click here for list.
| There are no comments on this article. Be the first to say your opinion. |






