How to make the parent menu get selected in multi level menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

How to make the parent menu get selected in multi level menu

Post by PHPycho »

How to make the parent menu get selected in multi level menu.

For example we have following multilevel menu:

Menu1
Menu2
- Menu2.1
- Menu2.2
-- Menu2.2.1
-- Menu2.2.2
Menu3
-Menu3.1

I generated the above menus from db table of following structure
id | parent_id|title
using recursive method.


I would like to know the logic of how to put some class(say class="active") in the parent level

menus if its child is selected
For example if Menu2.1 or Menu2.2.1 or any child menus is clicked then Menu2 should be selected.
if Menu3.1 is clicked then Menu3 should be selected.
(Note: the page is reloaded when clicked on the menu links)

Thanks in advance for any valueable suggestions/hints/logic.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: How to make the parent menu get selected in multi level menu

Post by aceconcepts »

Well if those menu links lead somewhere then you can (or should) have some code to determine which page to show.

If menu 2 = Shoes category then in your menu you can use a conditional statement to determine which menu is selected e.g.

Code: Select all

 
if($_REQUEST['category'] == "shoes"){ echo'class="active"'; } //this would go inside an <li> tag or something.
 
This is one way. Depends how you've structured your site though and how dynamic it is.

You can find JS scripts that do it well.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Re: How to make the parent menu get selected in multi level menu

Post by PHPycho »

I dont think this will work in multilevel menu.

Sorry aceconcepts i dont think your method works.
Suppose if i have Menu3.1 in the url then only Menu3.1 gets selected. But i want its parent gets selected.

anyway thanks.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: How to make the parent menu get selected in multi level menu

Post by aceconcepts »

It will work, it just depends how you code your site.

Either way there are lots of other ways to do it. Take a look at JavaScript.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Re: How to make the parent menu get selected in multi level menu

Post by PHPycho »

aceconcepts wrote:It will work, it just depends how you code your site.

Either way there are lots of other ways to do it. Take a look at JavaScript.
obviously .

But the concept that you have provided only works for parent level categories but when clicked on child level then it fails.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: How to make the parent menu get selected in multi level menu

Post by aceconcepts »

Like i said it depends how you code your site. CSS is very flexible if you know how to use it correctly.
Post Reply