Hi everyone

I have a menu that I'm creating dynamically with links that have query strings on the end of them. Basically I want the menu item, that's related to the page the user is on, to be in a different colour to the rest of the menu items.

An example of the links for the menu items follows:

fruit.php?type=1
fruit.php?type=2
fruit.php?type=3
fruit.php?type=4
fruit.php?type=5

Now using basename($_SERVER["REQUEST_URI"]) in an if statement makes all the menu items the colour that I want the menu item related to the current page to be.

I know that this is happening because all links are linking to fruit.php. Is there any way around this so that the specified query string is included in order to have one menu item a different colour instead of all of them?

Example of the if statement:

$toc = array("fruit.php?type=1" => "Apples", "fruit.php?type=2" => "Bananas", "fruit.php?type=3" => "Oranges", "fruit.php?type=4" => "Watermelons", "fruit.php?type=5" => "Pineapples");
foreach($toc as $item => $key){
    if(basename($_SERVER["SCRIPT_NAME"]) == $item){
        echo "<li><a class=\"currentNavSub\" href=\"".$item."\">".$key."</a></li>";
    }else{
        echo "<li><a href=\"".$item."\">".$key."</a></li>";
    }
}

I hope I've made some sense in this.

Recommended Answers

All 6 Replies

Hi there,
I assume your code snippet is from the fruit.php file, if thats the case then it is quite simple to do what (I think) you need, in fact you have already given it yourself: basename($_SERVER) will give the url sent to the server minus all of the folder names preceding the srcipt name.

For example, if my url sent was "www.website.com/folder/file.php?param=value" then basename($_SERVER) will give me "file.php?param=value", or in your case "fruit.php?type=X".

Hope this helped

Hi there,
I assume your code snippet is from the fruit.php file, if thats the case then it is quite simple to do what (I think) you need, in fact you have already given it yourself: basename($_SERVER) will give the url sent to the server minus all of the folder names preceding the srcipt name.

For example, if my url sent was "www.website.com/folder/file.php?param=value" then basename($_SERVER) will give me "file.php?param=value", or in your case "fruit.php?type=X".

Hope this helped

Hi Menster

Thanks but my code still gives all my menu items the class of "currentNavSub". I'm not sure if this is because all links are the same page or if "?type=x" is irrelevant. Not quite sure how to go about fixing this. Google hasn't been much help so far.

I retract my earlier statement.....it is in fact working.....thanks Menster for your input ;)

The suffix "?type=x" isn't irrelevant it's just that the code in your if statement, inside your foreach block, you are comparing the values to basename($_SERVER) which will always be "fruit.php" without parameters, you need to compare it to basename($_SERVER) which brings the parameters along with it.

commented: Thanks for the help ;) +2

In that case, sweet. Glad I could help (if I did)

Yes you did....reputation added ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.