Skip to main content
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Mike Brunt
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Mike Brunt ( @cfwhisperer )

Nesting CFLoop Tags Of The SAME Query... Hmmmm

By on
Tags:

Just of giggles (yes, this sort of stuff is fun for me), I did a little experimentation of how ColdFusion reacts when you nest CFLoop tags that are iterating over the same ColdFusion query object. I originally got onto to this because I was trying to write a custom tag that mimicked loop (also for fun). Anyway, the results are interesting.

First, I started off creating a simple query and getting a reference to the query. Since query objects are complex, the query is copied by reference which means the main query and the reference query are pointing to the same chunk of data:

<!--- Start out by creating a query object. --->
<cfset qOuter = QueryNew( "" ) />

<!--- Add a column and set default values. --->
<cfset QueryAddColumn(
	qOuter, "id", "INTEGER", ListToArray( "1,2,3,4,5" )
	) />


<!---
	Get a reference to the outter query, just for testing
	purposes. Since we are nesting queries, we want an outter
	reference to take away scope issues. This this is a query,
	it is passed by reference (not value).
--->
<cfset qOuterRef = qOuter />

As you can see, we are creating a simple query with a single ID column (values 1 - 5). For my first test, I did a CFLoop tag of type query on the query, qOuter. In this main loop, I am outputting the outer query CurrentRow, the ID at that row and the ID of the current row of the reference query. Then, I am also performing an inner CFLoop tag over the same query object and outputting the inner ID:

<!--- Loop over the outer loop. --->
<cfloop query="qOuter">

	<p>
		<!--- Display outer row. --->
		Outter Row: #qOuter.CurrentRow#<br />
		Outter ID: #qOuter.id#<br />
		Outter Ref ID: #qOuterRef.id#<br />

		<!--- Output the entire query. --->
		... Inner Loop:

		<!--- Loop over the outer loop again. --->
		<cfloop query="qOuter">

			[ #qOuter.id# ]

		</cfloop>
	</p>

</cfloop>

This gives me the following output:

Outter Row: 1
Outter ID: 1
Outter Ref ID: 1
... Inner Loop: [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

Outter Row: 2
Outter ID: 2
Outter Ref ID: 2
... Inner Loop: [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

Outter Row: 3
Outter ID: 3
Outter Ref ID: 3
... Inner Loop: [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

Outter Row: 4
Outter ID: 4
Outter Ref ID: 4
... Inner Loop: [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

Outter Row: 5
Outter ID: 5
Outter Ref ID: 5
... Inner Loop: [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

As you can see, the outer row, ID, and outer ref id all line up as would be expected.surprisedd this suprised me, the inner CFLoop acted just as you would *hope* it would act. And, at the end of the inner loop, the outer loop was able to stay on course (ie. it didn't get messed up at all by the inner loop.

I was very impressed with this. Two nested loop looping over the same object and maintaining their own cursor information (or iterator info, or however it works). Cool. Of course, I couldn't just let that happen. Now, I had to try to break it :)

In my next test, I ran the same exact loop, the only difference being that in the inner loop I output the outer reference ID. And, since the outer query and the outer ref query are the same object, the outer ref query *should* output the same data. Let's take a look:

<!--- Loop over the outer loop. --->
<cfloop query="qOuter">

	<p>
		<!--- Display outer row. --->
		Outter Row: #qOuter.CurrentRow#<br />
		Outter ID: #qOuter.id#<br />
		Outter Ref ID: #qOuterRef.id#<br />

		<!--- Output the entire query. --->
		...Inner Loop:

		<!--- Loop over the outer loop again. --->
		<cfloop query="qOuter">

			<!---
				This time, when looping, output the outer
				reference query id.
			--->
			[ #qOuterRef.id# : #qOuter.id# ]

		</cfloop>
	</p>

</cfloop>

This gives us the following output:

Outter Row: 1
Outter ID: 1
Outter Ref ID: 1
...Inner Loop: [ 1 : 1 ] [ 2 : 2 ] [ 3 : 3 ] [ 4 : 4 ] [ 5 : 5 ]

Outter Row: 2
Outter ID: 2
Outter Ref ID: 2
...Inner Loop: [ 1 : 1 ] [ 2 : 2 ] [ 3 : 3 ] [ 4 : 4 ] [ 5 : 5 ]

Outter Row: 3
Outter ID: 3
Outter Ref ID: 3
...Inner Loop: [ 1 : 1 ] [ 2 : 2 ] [ 3 : 3 ] [ 4 : 4 ] [ 5 : 5 ]

Outter Row: 4
Outter ID: 4
Outter Ref ID: 4
...Inner Loop: [ 1 : 1 ] [ 2 : 2 ] [ 3 : 3 ] [ 4 : 4 ] [ 5 : 5 ]

Outter Row: 5
Outter ID: 5
Outter Ref ID: 5
...Inner Loop: [ 1 : 1 ] [ 2 : 2 ] [ 3 : 3 ] [ 4 : 4 ] [ 5 : 5 ]

As you can see, the outer ref id (qOuterRef.id) value is still correct in the outer loop, however, in the inner loop, it is the same as the inner query loop information. So, it get's messed up (not *technically*) in a nested loop, but is STILL able to maintain the proper ID on the outer loop. I say that this isn't messed up technically because all of these query objects are the same reference and it makes sense that in the inner loop, the IDs be the same (just as they are the same in the outer loop.

Ok, so now for fun, I just wanted to try and break it by being mean. Now, at the end of each outer loop iteration, I am going to delete a record from the query:

<!--- Loop over the outer loop. --->
<cfloop query="qOuter">

	<p>
		<!--- Display outer row. --->
		Outter Row: #qOuter.CurrentRow#<br />
		Outter ID: #qOuter.id#<br />
		Outter Ref ID: #qOuterRef.id#<br />

		<!--- Output the entire query. --->
		...Inner Loop:

		<!--- Loop over the outer loop again. --->
		<cfloop query="qOuter">

			[ #qOuter.id# ]

		</cfloop>

		<!--- Delete the last query row. --->
		<cfset qOuter.RemoveRows(
			JavaCast( "int", (qOuter.RecordCount - 1) ),
			JavaCast( "int", 1 )
			) />
	</p>

</cfloop>

This gives us the following output:

Outter Row: 1
Outter ID: 1
Outter Ref ID: 1
...Inner Loop: [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

Outter Row: 2
Outter ID: 2
Outter Ref ID: 2
...Inner Loop: [ 1 ] [ 2 ] [ 3 ] [ 4 ]

Outter Row: 3
Outter ID: 3
Outter Ref ID: 3
...Inner Loop: [ 1 ] [ 2 ] [ 3 ]

Outter Row: 3
Outter ID:
Outter Ref ID:
...Inner Loop: [ 1 ] [ 2 ]

Outter Row: 2
Outter ID:
Outter Ref ID:
...Inner Loop: [ 1 ]

It's a pretty cool result. The outer loop still loops 5 times. This proves, once again, that tag attributes are evaluated only ONCE in a CFLoop tag and not for every iteration. The outer CurrentRow value is messed up by the fact that the underlying query is constantly beisurpassesed. By the time that the CurrentRow surpases the actual RecordCount, the ID values stop outputting. This, I suppose, is akin to outputting the value from an empty record set. The inner CFLoop tag still executes properly.

Anyway, no real point to this post. I just wanted to see what would happen.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel