Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tough problem involving a tree of links 1

Status
Not open for further replies.

nexius

Programmer
Joined
Jul 8, 2000
Messages
109
Location
CA
Hi

I'm starting work on a web page that stores a bunch of links to other sites, and it's divided into a large number of categories, each one with its own sub-categories...

So my plan was to represent this as a tree (similar to windows explorer) and whenever the user clicks on a category (to open it up) it submits the page to itself and re-calculates the new tree with all the new subcategories...

At the moment I'm storing all the links and categories in an SQL database... And so I need it to be dynamic (ie. I can easily add new links/categories)

Anyway, I'm really not sure how to do this. I've heard of creating session_id's in the database but I don't know much about it. Would that be a good idea?

I think the alternative would be to pass the new tree format through the browser... But since it needs to be dynamic, I don't know how this would work either.

If anyone can point me in the right direction (I just need some general advice, I'm not trying to find someone else to solve this problem for me) I would be _very_ appreciative.

THANKS
 
I assume that you have a database schema that will allow you to pull out the information you need in the way you need. Probably two tables, one that stores categories and one links. Each category has a "parent" value which defines its parent category -- a value of zero meaning it has no parent and is a topmost category. Each link entry has a column defining its parent category.

Whether or not you use sessions depends on whether the app is going to keep track of the last category you user opened, even when he leaves your site. If you're not going to do this, you probably don't need sessions.


Let's assume the following categories:

[tt]id CategoryName parent
1 Search Engines 0
2 News sites 0
3 Software 0
4 Win32 Software 3
5 Linux Software 3
6 Mac Software 3
[/tt]


One way to do this is to use category-tree paths.

When the script is run for the first time, it only produces a list of top-level categories. Something like:

<a href="yourscript.php?catpath=1>Search Engines</a>
<a href="yourscript.php?catpath=2>News Sites</a>
<a href="yourscript.php?catpath=3>Software</a>

When you click on one of these links, a $_GET['catpath'] will have a value in it. Looking for this variable will be how your script knows whether it was run with or without input.

Suppose your user clicks on "Software". The script should output the following:

<a href="yourscript.php?catpath=1">Search Engines</a>
<a href="yourscript.php?catpath=2">News Sites</a>
<a href="yourscript.php?catpath=3>Software<a>
<a href="yourscript.php?catpath=3_4>Win32 Software</a>
<a href="yourscript.php?catpath=3_5>Linux Software</a>
<a href="yourscript.php?catpath=3_6>Mac Software</a>

Notice the "catpath" values. It contains the entire category path to that category. The script can split the value at the underscore and know the parent of the clicked category. Using that information it can produce nested subcategory lists.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I wanted a menu similar to what you describe. I found the solution in a javascript, not php.
Look Here and you will find just what you need i think. sorry it's not php but i could not find one i liked in php
 
Thanks guys

I considered using javascript but I wanted a more customizable tree so I programmed my own...

Used something similar to sleipnir's solution. It works great now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top