I've done what you have done here with both CFC's and before the Custom
Tags.  It's fairly easy to right a recursive custom tag, since each call to
the tag gets it's own attributes scope.

This one is recursing over a Structure creating an indented tree select
list.  It could easily be modified to do the same thing over a query or any
other complex data type.

****************************
*** Recursive Custom Tag ***
****************************

<cfloop from="1" to="#ArrayLen(Attributes.Areas[Attributes.Parent])#"
index="i">
        <cfoutput>
                        <option
value="#Attributes.Areas[Attributes.Parent][i]['ID']#"#iif(ListContains(Attr
ibutes.Current,Attributes.Areas[Attributes.Parent][i]['ID']),DE('
selected'),DE(''))#>#Attributes.Spacer##i#)
#Attributes.Areas[Attributes.Parent][i]["Name"]#</option>
        </cfoutput>
        
        <cfif
StructKeyExists(Attributes.Areas,Attributes.Areas[Attributes.Parent][i]["ID"
]) AND Attributes.Parent NEQ Attributes.Current>
                <cfset Level = Attributes.Level + 1>
                <cf_areaselectlist areas=#Attributes.Areas#
parent=#Attributes.Areas[Attributes.Parent][i]["ID"]#
spacer="#Attributes.Spacer#&nbsp;&nbsp;&nbsp;" current=#Attributes.Current#
level = #Level#>
        </cfif>
</cfloop>

**** END ****

--------------
Ian Skinner
Web Programmer
BloodSource
Sacramento, CA


-----Original Message-----
From: Hagan, Ryan Mr (Contractor ACI)
[mailto:[EMAIL PROTECTED]
Sent: Wednesday, July 09, 2003 1:14 PM
To: CF-Talk
Subject: recursion in cold fusion ( was RE: Creating a list with
infinite groupings and indents )


This brings up an interesting question for me.  I tend to use recursive
functions frequently, and have even had to solve a problem very similar to
the original question posed in this thread.  My take on the solution is the
following (in php):


itemID    item            parentID
----------------------------------
     1    root                   0
     2    1st level 1            1
     3    1st level 2            1
     4    1st level 3            1
     5    2nd level 1 1          2
     6    2nd level 1 2          2
     7    2nd level 3 1          4
etc...

function list_categories( $parentId, $level ) {
        global $database, $connection;  
        $numSpaces = 5;
        $query = "SELECT * FROM myTable WHERE parentId = " . $parentId;
        $result = mysql_query($query, $connection);
        while( $row = mysql_fetch_assoc( $result ) ) {
                // output spaces for proper indentation
                for ( $spaces=0; $spaces<=($level*$numSpaces); $spaces++ )
                        echo '&nbsp;';
                echo '<br>\n';
                list_categories( $row["itemId"], $level+1 );
        }
}

// list entire table in tree format
list_categories( 0, 0 );



I personally like this solution quite a bit.  I know it has performance
issues, but I've never noticed any significant impact on server response,
even under heavy load.  To me, this is very clean and very readable code.
I'd love to be able to write this code in CFMX, but for several reasons,
it's just not possible.

First and foremost, we can't run queries inside of <cfscript> tags.  Bummer.

Second, even if the query issue were resolved (and if you've got ideas for
work-arounds, I'd love to hear them), you've now got variable scope issues.
A variable declared in a CFMX page is global (correct me if I'm wrong) to
the entire page, even inside of functions.  In other words, unlike PHP, CFMX
functions do not have their own scope separate from the rest of the page, so
in the above recursive function, I would be clobbering my "$result" variable
each time through the recursion.  I've got a workaround for this, but won't
go into here, just making a quick point.


So where does that leave us?  I'd be interested in some info on CFMX CFCs.
Can I use recursion in a CFC function?  If I could, it would certainly fix
the "no queries in <cfscript>" problem, however it seems as if it would
break all sorts of "good practice" rules for CFCs.  How about the second
point above?  Do CFCs have their own variable scope separate from the
calling page (and itself in the case of recursion)?

A lot of problems that are solved with recursion can be rewritten using
while loops.  For this particular problem, though, I've not been able to
come up with a non-recursive solution (assuming the above table structure).
Can anyone help me here?

Thanks for any and all insight into handling recursion in Cold Fusion.


-----Original Message-----
From: Sarah [mailto:[EMAIL PROTECTED]
Sent: Wednesday, July 09, 2003 1:42 PM
To: CF-Talk
Subject: RE: Creating a list with infinite groupings and indents


I think this is pretty much the same thing that others have pointed you 
towards, but I think this article might be easier to read:
http://www.sitepoint.com/article/1105

Sarah



At 7/9/2003 11:10 AM, you wrote:
>BTW Joe Clecko had an entire book on trees coming soon
>
>"TREES & HIERARCHIES IN SQL (Morgan-Kaufmann), 2003"
>
>http://www.celko.com/books.htm
>
>WG
>
>
>-----Original Message-----
>From: Michael T. Tangorre [mailto:[EMAIL PROTECTED]
>Sent: 09 July 2003 14:48
>To: CF-Talk
>Subject: Re: Creating a list with infinite groupings and indents
>
>
>First off... the way you have it setup now will only allow for two levels..
>level 1 and level 2. To get the ability you are after (tree type
strucutre),
>you need to get into nested sets. Joe Celko has done some awesome work in
>this area and the links below may help you out. It is a bit tricky at
first,
>but once you get it, it is very powerful. The sql statements for
>adding/removing/updating "children" or "parents" is already written in the
>links below and
>you can adjust them slightly to work for your situation. I would definitely
>start there.
>
>http://www.dbmsmag.com/9603d06.html
>http://www.mvps.org/access/queries/qry0023.htm
>
>HTH,
>
>Mike
>
>
>----- Original Message -----
>From: "John Sprenkle" <[EMAIL PROTECTED]>
>To: "CF-Talk" <[EMAIL PROTECTED]>
>Sent: Wednesday, July 09, 2003 9:19 AM
>Subject: Creating a list with infinite groupings and indents
>
>
> > Say I have a table with the following records:
> >
> > rec id      description          parent id
> > 1             Test Record 1      0
> > 2             Test Record 2      1
> > 3             Test Record 3      1
> > 4             Test Record 4      3
> > 5             Test Record 5      3
> > 6             Test Record 6      4
> > 7             Test Record 7      4
> > 8             Test Record 8      3
> >
> > The parent id field relates a record to its parent. I want to display
the
> > records in a list so that children records are listed indented under
their
> > parent. There can be an infinite number of nested parents and children,
so
>there
> > could be many indents in the list. The output in this example should
look
>like
> > this:
> >
> > Test Record 1
> >       Test Record 2
> >       Test Record 3
> >             Test Record 4
> >                  Test Record 6
> >                  Test Record 7
> >             Test Record 5
> >             Test Record 8
> >
> > Could somebody give me direction on how to program this? Sample code?
> >
> > Thank you.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq

Get the mailserver that powers this list at 
http://www.coolfusion.com

                                Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
                                

Reply via email to