RE: [PEAR] re: TreeMenu and Recursive algorithms
| From: | Joseph Blythe | Date: | Thu, 10 Apr 2003 07:54:51 +0000 |
| Subject: | RE: [PEAR] re: TreeMenu and Recursive algorithms | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-4762@lists.php.net to get a copy of this message | ||
Oh forgot to check the rest of the packages good point was going through
some of the new stuff when I updated PEAR the other day is looking great
some really neat stuff being developed out there in the PEAR community,
about to go home for the day but will do that first thing tomorrow
thankyou )
Regards,
Joseph
-----Original Message-----
From: Jeroen Houben [mailto:jeroen@terena.nl]
Sent: Thursday, 10 April 2003 5:12 PM
To: Joseph Blythe
Cc: pear-general@lists.php.net
Subject: Re: [PEAR] re: TreeMenu and Recursive algorithms
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
--
PEAR General Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php