Page 1 of 1

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

Posted: Sun Aug 23, 2009 6:20 am
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.

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

Posted: Sun Aug 23, 2009 7:53 am
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.

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

Posted: Sun Aug 23, 2009 11:10 am
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.

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

Posted: Sun Aug 23, 2009 7:15 pm
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.

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

Posted: Mon Aug 24, 2009 4:54 am
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.

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

Posted: Mon Aug 24, 2009 5:38 am
by aceconcepts
Like i said it depends how you code your site. CSS is very flexible if you know how to use it correctly.