This is a discussion on "Infinite sub-catagories" within the PHP Forum section. This forum, and the thread "Infinite sub-catagories are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Infinite sub-catagories
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
||||
|
||||
|
Infinite sub-catagories
Once again, aso got me thinking. Let's say I have a database full of sentences. A sentence may be the child of another sentence, but the parent sentence may also be a child. So you have virtually infinite sub-sentences. Now how could you take that, and print it out as a list?
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
|
|
|
|
#2
|
|||
|
|||
|
Re: Infinite sub-catagories
It's easy to represent in a database, but not so fun to parse with PHP. In the databse, you need (at least) 3 columns: sid (sentence id), pid (parent id), sentence
Each sentnece should have a unique sid. If the sentence is a root node, set pid to NULL. Otherwise, set it to it's parent's sid. "sentence" obviously stores the sentence. Now....to parse this bugger. Off the top of my head, here's a highly inefficient algorithm: Select all sentences with a NULL pid. For each of these, select all sentences that have a pid equal to its sid...... I hope you see where this is going. Do recursion. |
|
#3
|
||||
|
||||
|
Re: Infinite sub-catagories
I've already got the database thing down, the PHP I'm not sure how to do. Is it possible to access my_func() from within my_func()?
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
|
|
#4
|
|||
|
|||
|
Re: Infinite sub-catagories
Yes, that's called recursion.
Edit: Just make sure to escape when no more pid's are found or you can easily get yourself into an infinite loop. |
|
#5
|
|||
|
|||
|
Re: Infinite sub-catagories
Something like this (untested):
Last edited by darkzerox; Jul 2nd, 2008 at 23:39. Reason: Code tags, please, darkzerox |
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Infinite Loop | Bradster | PHP Forum | 2 | May 31st, 2007 08:18 |