Welcome Guest, Not a member yet? Register   Sign In
File Browser - Recursion Problem
#1

[eluser]eclectic01[/eluser]
Hello everyone,

I know the solution to this will be really simple and I'm a little embarrased to ask but I'm working to a deadline and I've wasted the past hour trying to solve the problem on my own so I thought it would be best to just ask if someone could point me in the right direction.

I've got a 'folder' table in my database:

Quote:folders (id, name, parent_folder)

I'd like to recursively go through the table and build something like this:

Folder A
Folder A1
Folder A2
Folder B
Folder B1
Folder B11
Folder B12
Folder B2
Folder C

Not quite sure what the exit condition for the recursive function should be!
#2

[eluser]stuart_g[/eluser]
This may help you:
http://articles.sitepoint.com/article/hi...a-database

For the site I developed I wanted to store a tree structure in a database (for the whole site structure and pages) and in the end used the excellent NSTrees library:
http://www.edutech.ch/contribution/nstrees/

This implements a nested set tree structure which is more efficient than the adjacency list you are using. But if you're in a rush, the adjacency list is probably easier to understand and implement.
#3

[eluser]WanWizard[/eluser]
It is possible to store a tree in a database like this, but it has some limitations, especially when the tree gets big and complex.

Read up on trees here: http://dev.mysql.com/tech-resources/arti...-data.html and here: http://articles.sitepoint.com/article/hi...a-database

I personally use nested sets, they are more flexible, but also slightly more complex to work with, due to the pointers you have to maintain.
#4

[eluser]eclectic01[/eluser]
Hi guys,

Thanks for the responses - I've looked into nested sets before - in fact I found an excellent library on the forums which I've used in the past:

http://ellislab.com/forums/viewthread/74114/

Due to time constraints, I wanted to use the adjacency list method to build a quick functional model that can be refactored and improved later on.

What exit condition could I put on a while loop for the recursive function - any ideas?
#5

[eluser]WanWizard[/eluser]
If you can define a limit on the number of levels in your tree, I would go for the query shown on the MySQL dev page. It returns the complete structure of your tree in one single query. Try to avoid running queries in a loop.
If you have to do it in a loop, I suggest running a 'select *', and than use a foreach to go through the results and build your tree in an array as you go. Make sure you order the result so that higher levels appear first in the result.
#6

[eluser]eclectic01[/eluser]
[quote author="WanWizard" date="1273779021"]If you can define a limit on the number of levels in your tree, I would go for the query shown on the MySQL dev page. It returns the complete structure of your tree in one single query. Try to avoid running queries in a loop.
If you have to do it in a loop, I suggest running a 'select *', and than use a foreach to go through the results and build your tree in an array as you go. <b>Make sure you order the result so that higher levels appear first in the result.</b>[/quote]

Thanks, I think the bolded part is missing from my bit of code.
#7

[eluser]WanWizard[/eluser]
Very important, if you process a lower level first, you can't add it to the proper place in the tree because its parent is missing.

Ordering only works if the records are in sequence, i.e. if children always have a higher ID then their parents. If not, you'll never be able to process this in a loop, you have to resort to the fixed-depth query in the MySQL example.

You said you were pressed for time, if you had implemented a nested tree, this would have been running by now... Smile




Theme © iAndrew 2016 - Forum software by © MyBB