Web Tutorials     [RO]  [EN]  
| HOME | Tutorials | News | SERVICES | Directory | Tools | FORUM | About | SITE MAP | CONTACT | SEARCH |
.....................................................
.....................................................
User happy birthdayToday we celebrate one day of birth.
(dannyb0y)
.....................................................
Login
Register
I forgot my password
.....................................................
HangMan Online Game
.....................................................
Online
In total there is
9 visitors online,
of which:
2 we have
7 are bots
.....................................................
Put your ad here
.....................................................
.....................................................
.....................................................
.....................................................
.
Home - Make menu selection active after url rewriting

<< Mozilla Firefox extensions for web developers   -   Install IIS on windows server >>
Rate this article(Members only)
1 2 3 4 5
A - A Announced this way the site administrator for any problems observed on this page.  Print this page as PDF  Email  

Make menu selection active after url rewriting


Publishing date: 11-02-2011 - Copyright © Claudiu Gilcescu-Ceia

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 licence

You can visit the author's website at blog.hazardousgaming.info

Publishing date: 11-02-2011 - Copyright © Claudiu Gilcescu-Ceia   
Click here if you want to see other articles by the same author

There are 2 similar articles, click here for list.

There are no comments on this article. Be the first to say your opinion.

Add a comment on this article (members only - login on the site):
Put your ad here