reference.
I query the table and loop it twice:
<cfscript>
function makeStruct(id, name, parent_id){
var stNew = StructNew();
stNew.id = id;
stNew.name = name;
stNew.parent_id = parent_id;
stNew.aChildren = ArrayNew();
return stNew;
}
stTree = StructNew();
stTree.aChildren = ArrayNew();
</cfscript>
<cfquery datasource="#application.dsn#" name="qTree">
SELECT ID, NAME, PARENT_ID FROM test
ORDER BY whatEverYouWant
</cfquery>
<!--- first loop: create structs --->
<cfloop query="qTree">
<cfset stTree[qTree.id] =
makeStruct(qTree.id,qTree.name,qTree.parent_id)>
</cfloop>
<!--- second loop: create hierarchy --->
<cfloop query="qTree">
<cfif qTree.parent_id IS 0>
<cfset ArrayAppend(stTree.aChildren,stTree[qTree.id]>
<cfelse>
<cfset
ArrayAppend(stTree[qTree.parent_id].aChildren,stTree[qTree.id]>
</cfif>
</cfloop>
You start with stTree.aChildren for the first level. This array contains
structures with all the info on the element (id, name, parent_id) and an
array aChildren (can be empty) with the children. And so on. If you have
a lot of elements, don't try dumping the structure. But because structs
are passed by reference, this doesn't take much more place in memory
than the query.
You will probably need a recursive UDF or CT to write the html.
> -----Original Message-----
> From: Paul Wilson [mailto:[EMAIL PROTECTED]
> Sent: woensdag 2 juni 2004 8:22
> To: CF-Talk
> Subject: Recursive function and Hierarchical navigation
>
> I'm trying to build a dynamic hierarchical navigation system
> using thew following table structure
>
>
> ID NAME PARENT_ID
> 1 category1 0
> 2 category1_1 1
> 3 category1_2 1
> 4 category1_1_1 2
> 5 category1_1_2 2
> 6 category2 0
> 7 category3 0
>
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

