One loop placed inside another creates a nested loop. Generally speaking, the inside loop completely iterates for every single iteration of the outside loop. The following nested Index loops help clarify the situation: <cfloop index="Outside" from="1" to="2"> <cfloop index="Inside" from="7" to="9"> <cfoutput> Outside is: #VARIABLES.Outside# Inside is: #VARIABLES.Inside#<br> </cfoutput> </cfloop> </cfloop> The outside loop initializes to 1, followed by the inside loop initializing to 7; then the output is displayed. The first </cfloop> causes the inside, or nested, loop to increment to 8, and the output is displayed. This process continues until the inside loop terminates. Then the second </cfloop> is processed and increments the outer loop to 2. When the inside <cfloop> is seen this second time, it is as if it has not been seen before, so it initializes the variable Inside to 7. The output of the loops is as follows: Outside is: 1 Inside is: 7 Outside is: 1 Inside is: 8 Outside is: 1 Inside is: 9 Outside is: 2 Inside is: 7 Outside is: 2 Inside is: 8 Outside is: 2 Inside is: 9 You can nest other types of loops by applying the same logic. TIP You can place code between the two beginning <cfloop> and/or the </cfloop> tags that create the nested loops. This capability can be especially helpful for formatting issues such as building HTML tables. |