Re: re: TreeMenu and Recursive algorithms
| From: | Jeroen Houben | Date: | Thu, 10 Apr 2003 07:41:48 +0000 |
| Subject: | Re: re: TreeMenu and Recursive algorithms | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-4761@lists.php.net to get a copy of this message | ||
Joseph Blythe wrote:
Hello everyone ) Was wondering if anyone could help me with a problem I am having with trying to generate a HTML_TreeMenu based on a database table of cascading categories my maths is real bad :) so I am having problems trying to work out a recursive algorithm to loop through and build the tree so far I have managed to get two levels of it working which is hardly recursive. I have a mySQL database table: id tinyint(4) unsigned NOT NULL auto_increment, category varchar(50) NOT NULL default '', predcessor tinyint(4) unsigned default NULL, PRIMARY KEY (id), UNIQUE KEY category (category) Basically the predcessor if not empty points at another category id. Building the tree: ---snip---> while($row = $result->fetchRow(DB_FETCHMODE_OBJECT)) { if ($result->numRows() > 0) { // Find all Parent nodes if (empty($row->predcessor)) { $var = "node" . $row->id; $$var = new HTML_TreeNode(array('text' => $row->category, 'link' => "editCategory.php?id=$row->id", 'icon' => $icon, 'expandedIcon' => $expanded_icon)); } else { // Find all child nodes $i = 0; while($row = $result->fetchRow(DB_FETCHMODE_OBJECT)) { $var1 = "node" . $row->predcessor; $var2 = "child" . $i; if (!empty($row->predcessor)) { $$var2 = &$$var1->addItem(new HTML_TreeNode(array('text' => $row->category, 'link' => "editCategory.php?id=$row->id", 'icon' => $icon, 'expandedIcon' => $expanded_icon))); } $i++; } } $menu->addItem($$var); } else { echo "Sorry no categories found."; } } } <---snip---- Any suggestions would be much appreciated I hope you can make some sense from my madness ) Regards, Joseph Blythe mailto:joseph@optcom.net.au Hi Joseph,You should have a look at PEAR::Tree (http://pear.php.net/package-info.php?pacid=104). It does all the recursive stuff for you and if I'm not mistaking PEAR::TreeMenu can even take a PEAR::Tree instance and generate the menu from that tree. HTH Jeroen