[eluser]Jondolar[/eluser]
If you can store your data differently, you can look at the concept of a nested set. Once your data is stored in the database as a nested set, you can extract everything you need with one query. There are nested set classes available.
Good luck with your project.