Re: re: Tree navigation via links
| From: | Wolfram Kriesing | Date: | Mon, 28 Apr 2003 08:44:11 +0000 |
| Subject: | Re: re: Tree navigation via links | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-5096@lists.php.net to get a copy of this message | ||
Hi Joseph,
I think you need to use the Dynamic_DB* interface for your tree, this way you can read out the data you really need.
Please see
http://www.phpkitchen.com/article.php?story=20030427152620585
for a introductory tutorial
Your problem, you might attack it like this:
$root = $tree->getRoot();
$rootId = $root['id'];
// get the first level, that means all the children of the root
$firstLevel = $tree->getChildren($rootId);
// get the entire path to your current element
// where the $_REQUEST['id'] is simply the element the user had
// clicked on
$path = $tree->getPath($_REQUEST['id']);
// and to show the user the children of the current item
// you might want to do this
$children = $tree->getChildren($_REQUEST['id']);
now you only have to render the data :-)
properly
therefore you might want to build an array like this which you simply have to loop through later ...
// the very first item is always the root, i guess
// if you dont need the root, simply leave out this line
$items = array($root);
foreach ($firstLevel as $aNode) {
$items[] = $aNode;
// add the $path at the appropriate place
if ($aNode['id'] == $path[1]['id']) {
array_shift($path); // remove the root
array_shift($path); // remove the node which is equal $aNode
array_merge($items,$path); // add all the other items
}
}
i havent tried the code, but i hope i could transport the idea.
i know it should be easier. if any of you has an idea how to make it easier, may be adding a method in Tree, which does that ... your input is welcome
regards
Wolfram
Joseph Blythe wrote:
Hey all, I was wondering how one would go about navigating a PEAR Tree structure by using links, what I would like to be able to do is list all the top level nodes as links and as the user clicks through each one it would pass it's id and list all of its children (using the same page). I do not want to display the whole tree at one time, only the top level nodes then each child. The reason I am thinking of this is I have a bunch of product categories in a DB and need a easy way to allow a user to navigate them, because the categories are content managed and infinite I have to have to find a way to navigate them to the product level easily no matter how many categories it takes to reach them. I have a working Tree_Menu, using the same data but this is far from what I want on the display/navigation side, I hope this makes some sense if anyone has any suggestions/examples or just want to tell me how stupid I am then feel free ) I guess I was thinking of maybe trying to extend Tree_Menu to allow this kind of navigation but I must admit is probally beyond me at the moment. Any help appreciated, Regards, Joseph Blythe-- Wolfram ... opensource @ vision:produktion ... http://opensource.visionp.de ... translating template engine .... http://sf.net/projects/simpletpl ... authentication system .... http://sf.net/projects/auth