Nesting CFLoop Tags Of The SAME Query... Hmmmm

Posted October 6, 2006 at 2:36 PM

Tags: ColdFusion

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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »