Nested Cfloop tags scope resolution

posted April 28th 2004 at 1352 EDT in All, ColdFusion

There is a bug in the processing of nested cfloop tags in Macromedia ColdFusion MX, as the example below shows. I have developed a workaround for this which is also below.

Example of Problem

<cfloop query="outerqueryset">   
    <cfloop query="innerqueryset">     
        <cfoutput>#outerqueryset.column1# #innerqueryset.column1#<br></cfoutput>
    </cfloop>
</cfloop>

outerqueryset.column1‘ results in the first record’s data being displayed regardless of current iteration in the outer loop.

Workaround

The workaround is to force the display of the current row, by accessing it via array.

<cfloop query="outerqueryset">
    <cfloop query="innerqueryset">
        <cfoutput>#outerqueryset.column1[outerqueryset.currentrow]# #innerqueryset.column1#</cfoutput>
    </cfloop>
</cfloop>

6 Responses

  1. #1 javeiria
    3 years, 2 months ago

    thanks for code

  2. #2 Clint
    3 years, 1 month ago

    You can also deal with this by making a copy of the variables you need from the out query before the inner loop begins, and then use the copies of the variables within the inner query.

  3. #3 me
    2 years, 11 months ago

    good idea

  4. #4 Justin
    2 years, 5 months ago

    Thanks for the code!!! I’m actually using it to get around this same bug that still exists in CFMX 7.01 in a situation where I use queries to dynamically figure out what the variable names are for both the outer and inner query to read-in an excel file with user-specified column names.

    In case anyone else is interested here’s what the looping code looks like:

  5. #5 Pam
    1 year, 11 months ago

    Thanks! Works great! You saved my ….

  6. #6 bardicknowledge
    1 year, 9 months ago

    Thanks so much for that tip! It has helped a lot. As Justin has pointed out, they still haven’t fixed that bug.

Leave a comment