Page 1 of 1

Retrieving a category Tree starting from Certain LEAF

Posted: Sun Feb 05, 2006 10:18 am
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]

Posted: Sun Feb 05, 2006 10:30 am
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.