CF Loop Bug

I've seen this numerous times throughout the years but I've never actually written it down.

When you do a CFLOOP within a query loop, odd things occasionally happen.

For example:

<cfoutput query="getWidgets">
<select name="whatever">
<cfloop query="getThings">
      <option value="#getThings.ID#" <cfif getThingsID EQ getWidgets.ThingID>selected </cfif>>#getThings.Name#</option>
</cfloop>
</select>
</cfoutput>

The value for getWidgets.ThingID will be lost!

So you have to patch it up like so:

<cfoutput query="getWidgets">
<select name="whatever">

<!--- This line will fix the problem --->
<cfset foo = getWidgets.thingID>

<cfloop query="getThings">
      <option value="#getThings.ID#" <cfif getThingsID EQ foo>selected </cfif>>#getThings.Name#</option>
</cfloop>
</select>
</cfoutput>

This happens in pretty much all versions of CF I've tested on, including v7.

I don't think this happens EVERY time, just in certain situations. Irritating? Hell yeah.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)

Psykel blog uses BlogCFC (by Raymond Camden). Layout design inspired by arcsin