Chris W. Parker wrote:
Hi everyone,
I've been working on a problem for a few days now and I'm not making any headway so I think it's time I come to the list for some help (though this really disappoints me since it appears I'm not capable of solving this problem on my own!).
Anyway, I'm using the Modified Preorder Tree Traversal method to store my category hierarchy. Using recursion I can build an array that accurately depicts the layout of the categories. So far so good. What I have not been able to do thus far is turn that array into a list that looks like this:
Food:Fruit:Red Food:Fruit:Green Food:Fruit:Yellow Food:Vegetables:Long Food:Vegetables:Round Food:Vegetables:Round:Spikey Food:Vegetables:Round:Smooth
My array is included at the end of this email. (And yes I typed it by hand so hopefully there aren't any errors in it.)
I've searched the web but haven't found anything that's helped.
Anyone have a solution?
Thanks, Chris.
...
I'm assuming this *is* a in a DB with Left and Right values.
There are two ways I do this, the first is just a relatively simple query:
SELECT sName FROM table WHERE 5 != iID AND (SELECT iLeft FROM table WHERE 5 = iID) BETWEEN iLeft AND iRight ORDER BY iLeft;
That will get You the parents of the Node with ID 5, starting witht he Root node. It pulls out all the Nodes whose Left and Right values contain the target node.
I also do this otuside of the query sometimes like this:
$oBy is an Object that implements Iterator and returns MySQL database rows as objects. It maintains the Path parts as a stack
$aStack = array();
$aPath = array();
$iDepth = 0;
foreach($oBy as $oRow)
{
while($iDepth > 0)
{
if($aStack[$iDepth-1] < $oRow->iR) unset($aStack[--$iDepth],$aPath[$iDepth]);
else break;
}
$aPath[$iDepth] = $oRow->sCategoryID;
echo implode(':',$aPath);
$aStack[$iDepth++] = $oRow->iR;
}
I've been doing a lot of working with these things the last few weeks, alot of this stuff is still fresh in mind, but I'm a horrible communicator. If this didn't answer your question, or if you ihave more ask away, I'll do my best.
Chris
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php