re: TreeMenu and Recursive algorithms
| From: | Joseph Blythe | Date: | Thu, 10 Apr 2003 01:12:15 +0000 |
| Subject: | re: TreeMenu and Recursive algorithms | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-4754@lists.php.net to get a copy of this message | ||
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