Whenever I run across one of these problems, I
map the tree to a structure. Each node in the
structure is a structure itself, with the
following keys: id (same as the key), name,
parent (pointer to the parent node), and children
(array of pointers to the child nodes).
This gives me maximum flexibility. I can walk
up or down the tree from any starting point. The
subcategories are automatically sorted based on
whatever I use for the order by clause in the
SQL statement.
Below is the code.
Patrick
-------
<cfquery datasource="#request.DSN#" name="qryCats">
SELECT catID, Name FROM Categories
ORDER BY Name
</cfquery>
<cfset categories = structnew()>
<cfloop query="qryCats">
<cfscript>
if (structKeyExists(categories, qryCats.CatID)) {
temp = categories[qryCats.CatID];
} else {
temp = structNew();
temp.id = qryCats.CatID;
temp.children = arrayNew(1);
}
if (structKeyExists(categories, qryCats.parentID)) {
parent = categories[qryCats.ParentID];
} else {
parent = structNew();
parent.id = qryCats.ParentID;
parent.children = arrayNew(1);
categories[qryCats.ParentID] = parent;
}
temp.parent = parent;
arrayAppend(temp.parent.children, temp);
temp.name = qryCats.Name;
temp.description = qryCats.description;
categories[qryCats.CatID] = temp;
</cfscript>
</cfloop>
------
> -----Original Message-----
> From: Steve Nelson [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, January 24, 2001 12:13 PM
> To: Fusebox
> Subject: Re: Sorting, showing products in the Nested Trees
> (secretagents.com)
>
>
> We've been trying to figure that out ourselves! ;) I've been chatting
> a little with Joe Celko about this. It's a weird problem because the
> data is denormalized. I'm sure it can be done, we just need to figure
> out how!
>
> Here is some stuff David Medinets has put together on the nested set
> model. We're going to redo the tutorials with his findings:
>
> http://medinets.onproject.com/ntm/ntm.htm
>
> He did them all in Stored Procedures which is cool, makes them a little
> faster.
>
> Steve
>
> Marc Funaro wrote:
> >
> > Hi everyone,
> >
> > I have begun using nested trees for display of heirarchical data in my
> > fusebox app, and have a couple of questions:
> >
> > 1. My output is working fine, but I would like to sort the
> categories and
> > subcategories alphabetically. Is there any way to alter the
> queries so that
> > i can do this?
> >
> > 2. The sample application displays only categories, however
> the technique
> > has been used on the secretagents site to output categories AND
> products. I
> > know how I could do this using an additional query *during*
> output of the
> > categories, but it seems inefficient -- one of the "selling
> points" of this
> > technique is the speed benefit over the more traditional means
> of creating
> > heir. structures. Is there a way to integrate a products table into the
> > query used to display the tree, so that the entire output is
> created with
> > one query?
> >
> > Thanks in advance for any assistance y'all can provide!!
> >
> > .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
> > Marc Funaro, President
> > Advantex Technical Consulting Services
> > 5547 State Highway 12
> > Norwich, NY 13815
> > Phone: 607-336-6895
> > Fax: 801-383-4864
> > Internet: http://www.advantex.net
> > Email: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> >
> > "You know, I have one simple request... and that is,
> > to have SHARKS with Frickin' LASER BEAMS attached
> > to their HEADS..."
> >
> > --Dr. Evil
> > .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
> >
> >
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at
http://www.fusionauthority.com/bkinfo.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists