Skip to main content
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Mark Drew
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Mark Drew ( @markdrew )

Standard Query Iteration Values Available During For-In Loop In ColdFusion

Published in

A decade ago, ColdFusion introduced the for-in loop syntax for Query iteration. And, in the decade since, I somehow completely missed the fact that the underlying query iteration mechanics were still being used. That is, the .currentRow and default value accessors are still made available on the query object within the for-in iteration block. This allows us to access both the row object and the row index using the for-in syntax.

To see what I mean, let's manually construct and populate a Query object and then iterate over it using for-in. Within each iteration, we'll output both the row-based data and the query-based data:


	data = queryNew(
		"id, value",
		"varchar, varchar",
			[ id: 101, value: "Hello" ],
			[ id: 102, value: "my" ],
			[ id: 103, value: "good" ],
			[ id: 104, value: "fellow" ]

	for ( record in data ) {

		// When using a FOR-IN loop over a Query object, we usually just use the row
		// object to access our information.
		echo( );
		echo( " &rarr; " );
		echo( record.value );
		echo( "<br />" );

		// HOWEVER, under the hood, the ColdFUsion runtime is still ITERATING over the
		// QUERY object, which means that traditional access pathways are still available
		// (ie, the currentRow is updated and the default column-value points the current
		// row if no row-index is provided).
		echo( data.value );
		echo( " @ " );
		echo( data.currentRow );
		echo( "<br /><br />" );



As you can see, inside of our for-in query, we're using both the row references:

  • record.value

... as well as the traditional query-based iteration references:

  • data.currentRow
  • data.value - with the row-index implicitly pointing to the current row.

And, when we run this in Adobe ColdFusion 2021 and Lucee CFML 5, we get the following output:

101 → Hello
Hello @ 1

102 → my
my @ 2

103 → good
good @ 3

104 → fellow
fellow @ 4

As you can see, even though we're not using the traditional CFLoop[query] iteration, the for-in syntax is still applying the traditional iteration mechanics behind the scenes. This allows us to reference the .currentRow while using the more modern query iteration syntax. Pretty cool! Not sure how I missed this for the last 10-years.

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

Reader Comments

Post A Comment — I'd Love To Hear From You!

Post a Comment

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