Retrieving a category Tree starting from Certain LEAF

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mbaroz
Forum Commoner
Posts: 29
Joined: Sun Feb 05, 2006 10:10 am

Retrieving a category Tree starting from Certain LEAF

Post by mbaroz »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi 
I have a small problem.
I have an N level Category Table on Mysql  : CatID | CatName | Parent
I would like to use a function (maybee recursive) to retreive all Sub and sub-sub.. categories from it starting from [b]SPECIFIC [/b]CatID 

The Problem is that when i want to collect all the Subcategories ID's (and/Or SubCats Name) into an array and return it from the function , it doesnt work. But when printing the current leaf within the function it is working .
 
here is the code:(the function GetAllCategories(); retrieves a multi dimentional array with all categories)

Code: Select all

function GetSubCats($parent) {
	if (!$count) $count=0;
	
	$CATS=GetAllCategories();
	$categoryCount=count($CATS[CatID]);
	
	 for ($i=0; $i<$categoryCount; $i++) {
		 
	  	if ($CATS[Parent][$i] == $parent) {
	   		
	   		$S[CatID].=$SUBS[CatName][$i]."|";
	   		GetSubCats($CATS[CatID][$i]);
	   		
	   	} //end if
	   
	   	  
 	} //end for
	return $s;
}


Please help ...
Thanks
moshe


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have to actually store the return information from GetSubCats() in your recursion.

Another thing of note, instead of things like

Code: Select all

$SUBS[CatName][$i]
make sure to quote string literals

Code: Select all

$SUBS['CatName'][$i]
otherwise you are generating errors (which may or may not be seen) but none-the-less fire.
Post Reply