Skip to main content
Ben Nadel
It's not enough; but, not enough is better than nothing.
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Adam Tuttle and Laura Arguello
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Adam Tuttle@adamtuttle ) and Laura Arguello

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

By Ben Nadel on
Tags: ColdFusion

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:

<cfscript>

	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( record.id );
		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 />" );

	}

</cfscript>

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

  • record.id
  • 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.



Reader Comments

What has two thumbs and hopes you leave a comment? This Guy! (Ben Nadel).

Post A Comment

You — Get Out Of My Dreams, Get Into My Blog
Live in the Now
Oops!
NEW: Some basic markdown formatting is now supported: bold, italic, blockquotes, lists, fenced code-blocks. Read more about markdown syntax »
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.