CodeIgniter Forums
Tree navigation ... need some help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Tree navigation ... need some help (/thread-33202.html)



Tree navigation ... need some help - El Forum - 08-18-2010

[eluser]Warz[/eluser]
Hi,

I'm making a tree navigation.

Like this:
Code:
- Home
|-Account
  |- Edit Profile
  |- Edit Password
|-Forum
  |- Create topic
  |- Search
|+Blog
|+Help
This is example of how I did the table:
Code:
id      |parent_id|lft|rgt|order_id|          name|                         link
--------+---------+---+---+--------+--------------+-----------------------------
1       |        0|  X| XX|       1|          Home|
--------+---------+---+---+--------+--------------+-----------------------------
2       |        1|  X| XX|       1|       Account|
--------+---------+---+---+--------+--------------+-----------------------------
3       |        2|  X| XX|       2| Edit Password| users/profile/password/edit
--------+---------+---+---+--------+--------------+-----------------------------
4       |        2|  X| XX|       1|  Edit Profile| users/profile/edit
--------+---------+---+---+--------+--------------+-----------------------------
5       |        1|  X| XX|       3|          Blog| blog
--------+---------+---+---+--------+--------------+-----------------------------
6       |        5|  X| XX|       1| See all blogs| all
--------+---------+---+---+--------+--------------+-----------------------------
7       |        1|  X| XX|       2|         Forum| forum
--------+---------+---+---+--------+--------------+-----------------------------
8       |        7|  X| XX|       1|  Create topic| create
--------+---------+---+---+--------+--------------+-----------------------------
9       |        7|  X| XX|       2|        Search| search
--------+---------+---+---+--------+--------------+-----------------------------
10      |        1|  X| XX|       4|          Help| help
--------+---------+---+---+--------+--------------+-----------------------------
lft and rgt is just something I use to generate tree structure without using recurssive function and it doesn't matter here hence I replaced those with X.

When the menu is generated it will be with the following links:
Quote:id: 1 - no link
id: 2 - no link
id: 3 - http://domain.com/users/profile/password/edit
id: 4 - http://domain.com/users/profile/edit
id: 5 - http://domain.com/blog
id: 6 - http://domain.com/blog/all
id: 7 - http://domain.com/forum
id: 8 - http://domain.com/forum/create
id: 9 - http://domain.com/forum/search
id: 10 - http://domain.com/help

As you can see it adds up each link from all parents....

Now the problem, I want to use class="selected" to mark what page we are currently on.

The problem is I do have links not stored in the tree structure meaning that I cannot simply compare the URL string with link because then, if the link is not saved in database I will end up not having anything selected.

Imagine this:
http://domain.com/forum/topic/edit

It's a link not stored in the tree structure, and in this case I want "Forum" to be the one with the class="selected".

Basically, I'm wondering does anyone know of a good solution to making a navigation system?

I tried for many days thinking out the best way to do this, but I end up not happy with it.