

Kyle Godard
28
Jan 2010
A better way to handle variables when nesting CFLOOP
Tags: cfloop, ColdFusion, Web Development, Anything Dev
0 comment(s) | If you try to reference a value from the outer query within a nested CFLOOP you normally get only the first value from the outer query rather than the value of the current row. In the past a common solution has been to store a temporary value before the nested loop.
<cfloop query="qStates">
<cfset tempState = qStates.State>
<cfloop query="qCities">
#qCities.City#, #tempState#
</cfloop>
</cfloop>
But this can become cumbersome when dealing with several values and a layer of complexity when reading through the code. A better way to do this is to access the outer query directly using the current row.
<cfloop query="qStates">
<cfloop query="qCities">
#qCities.City#, #qStates["State"][qStates.CurrentRow]#
</cfloop>
</cfloop>

Click to get started.