CFLOOP inside of a CFOUTPUT
Previously, I wrote about using a CFLOOP inside of a CFLOOP. Here's another solution for a quirky problem you might encounter.
Sometimes a situation exists where you need to use a CFLOOP inside of a CFOUTPUT. I had two queries, the first was the main query (queryOneList), and the second (queryTwoList) was going to be used to dynamically create the columns in a table for an administration area.
This is a pretty unique situation, and it created a unique problem. Below is the code I initially wrote:
<cfoutput QUERY="queryOneList">
<tr>
<td>#queryOneList.MEDIUM_NAME#</td>
<td>#queryOneList.MEDIUM_DESC#</td>
<cfloop query="queryTwoList">
<td><A href="xyz.cfm?mn=#queryOneList.MEDIUM_NAME#&code=#queryTwoList.CODE#">#queryTwoList.CODE_NAME#</A></td>
</cfloop>
</tr>
</cfoutput>
</table>
The problem was that since I scope all variables, the queryOneList.MEDIUM_NAME value INSIDE THE CFLOOP was always the first value returned from "queryOneList". Obviously, I needed to reference that row's value. I quickly solved this problem by referencing the value as so in the url link:
<cfoutput QUERY="queryOneList">
<tr>
<td>#queryOneList.MEDIUM_NAME#</td>
<td>#queryOneList.MEDIUM_DESC#</td>
<cfloop query="queryTwoList">
<td><A href="xyz.cfm?mn=#queryOneList.MEDIUM_NAME[queryOneList.currentRow]#&code=#queryTwoList.CODE#">#queryTwoList.CODE_NAME#</A></td>
</cfloop>
</tr>
</cfoutput>
</table>
Another solution would have been to create another variable on the first line after the CFOUTPUT with the value, and referencing it:
<cfset theNameIs = queryOneList.MEDIUM_NAME>
<tr>
<td>#queryOneList.MEDIUM_NAME#</td>
<td>#queryOneList.MEDIUM_DESC#</td>
<cfloop query="queryTwoList">
<td><A href="xyz.cfm?mn=#theNameIs#&code=#queryTwoList.CODE#">#queryTwoList.CODE_NAME#</A></td>
</cfloop>
</tr>
</cfoutput>
</table>
Either way will work I guess, but I prefer the first method. It's more pro in my opinion.


They said it was by design and it does not appear they are fixed it anytime soon.
This has caused me a lot of head-aches over the years as if you access outer query inside of inner loop will always get first record.
Alternative is to do your inner query with for loop.