|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with recursive function
Hi,
I have categorys and subcategories in one table with a parent/child relationship. I want to be able to determine the path UP to the top level category from any sublevel category. And develop an array of subcategories along the way up to top level I have the code below *sort of* working but not completely, and I wondered if anyone might have a moment to point me in the right direction thank you my code: PHP Code:
the output from above will display this: Array ( [ID] => Array ( [0] => 1 ) [Name] => Array ( [0] => Aquatics ) ) what I would like is to do this for each of the categories as I go up the ladder |
|
#2
|
||||
|
||||
|
I could be of more use if I could see the result of the following SQL query.
Code:
SQL
DESCRIBE t_category_cat; I'm assuming that's the table name. In the code above, you're missing the last underscore.
__________________
PHP Guru -- jQuery Novice -- SQL Master -- RHCE Skudd.com Blogs -- More fun than a barrel full of monkeys! |
|
#3
|
|||
|
|||
|
Quote:
Hi thanks for helping out, here's the way that table is setup: PHP Code:
basically the structure would look like Aquatics ....preschool ........instruction ....youth ........instruction ........training ....adult ........advanced ........etc... |
|
#4
|
|||
|
|||
|
PHP Code:
Notice that the cPath parameter is passed by reference! Usage: PHP Code:
Bobby |
|
#5
|
|||
|
|||
|
Wow! that is awesome! thanks so much for the help, really nice piece of scripting! I'm not advanced enough to know about passing by reference, but I need to check that out a bit to understand how you got that to work so well.
thanks again |
|
#6
|
|||
|
|||
|
It's not advanced nor is it my best work
However, it demonstrates 1 method of implementing a recursion function. Another method would be to return the data instead of using referenced variables. I find that the referenced parameter recursion functions are easier to use but your mileage may vary.Another very important aspect of creating recursion functions is to make damn sure you don't get into an infinite loop. Use recursion break constructs (return, break, continue, etc) as liberally as possible! Enjoy! Bobby Last edited by Bobby_Easland : September 28th, 2005 at 09:38 AM. Reason: Spelling correction |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > PHP Development > Help with recursive function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|