This is essentially the reason recursion was created.
Recursion (I may get this wrong... I don't have a Comp-Sci degree... or any
degree actually) is the concept of "self referencing code". In simplest
terms code which calls itself.
In CF you can do this with any extension that maintains its own private
scope: a function or a custom tag (or a function in a CFC or Custom tag).
Since the code will be calling itself you need a private place for each call
to maintain it own state.
Let's take a simple example that's related to what you're doing. I have a
simple black box (a function) that accepts a name and displays the children
of that person. It looks like this:
getChildren(CurrentName) {
CurrentChildren = magicDataCall(CurrentName)
Loop over CurrentChildren
Display Current Child in Loop
End Loop
}
I hope that makes some sense. It just calls for data (magicDataCall in this
case) and loops over the results displaying them as it does.
But now my problem has changed. I don't want just the children, I want all
the descendents. Now I don't know how my levels of descendents there are
(sound familiar?)
So I remodel the function as a recursive function like so:
getChildren(CurrentName) {
CurrentChildren = magicDataCall(CurrentName)
Loop over CurrentChildren
Display Current Child in Loop
getChildren(CurrentChildName)
End Loop
}
Note that after displaying a child I call the SAME FUNCTION with that childs
name.
If I call that function with my name (I have two kids and they have no kids)
I'd get:
Jim // Start
Paxton // Child of Jim
Matilda // Child of Jim
However if I call it with my MOM's name I'd get this (I have a brother and
he has a kid):
Sandy // Start
Joe // Child of Sandy
Sara // Child of Joe
Jim // Child of Sandy
Paxton // Child of Jim
Matilda // Child of Jim
If this is confusing don't worry - work it out on paper and you'll get it
(or have a stroke... either way, probably solved!)
So, for your problem you don't need to know how many loops you'll have: you
just need to make a function that accepts a CatID, gets the children then
calls the SAME function for each child.
Remember however that in CF function variables must be prefixed with the VAR
keyword to make them private: this is VERY IMPORTANT when building recursive
functions!
Also - a minor point, but the HTML you're producing in your example is
broken (<ul> isn't allowed in a <ul> - but it is allowed in a <li>.) The
basic concepts of recursion are pretty simple.... but getting the output
right can be a major pain the ass. ;^)
Jim Davis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Find out how CFTicket can increase your company's customer support
efficiency by 100%
http://www.houseoffusion.com/banners/view.cfm?bannerid=49
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:227711
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54