Ask Ben: Query Loop Inside CFScript Tags

Posted July 25, 2006 at 8:54 AM

Tags: ColdFusion, Ask Ben

I know I can loop over a query using the loop tag, but is there a way to do a query loop inside of script tags?

Yes. The ColdFusion query object is a specialized record set structure, but really you can think of it as a glorified array of sorts. The trick here lies in the fact that, like an array or structure, you can access values in a ColdFusion query through the use of keys and indexes.

This idea of key/index accessing can be used when both looping over a query and creating one from scratch. To help demonstrate this, let's start by building a small query from scratch:

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

  • <!--- Create a new query. --->
  • <cfset qPornStars = QueryNew( "name, sex, preferred_category" ) />
  •  
  • <!--- Add rows to this query. --->
  • <cfset QueryAddRow( qPornStars ) />
  • <cfset qPornStars[ "name" ][ 1 ] = "Jenna Jameson" />
  • <cfset qPornStars[ "sex" ][ 1 ] = "Female" />
  • <cfset qPornStars[ "preferred_category" ][ 1 ] = "Girl on Girl" />
  •  
  • <cfset QueryAddRow( qPornStars ) />
  • <cfset qPornStars[ "name" ][ 2 ] = "Peter North" />
  • <cfset qPornStars[ "sex" ][ 2 ] = "Male" />
  • <cfset qPornStars[ "preferred_category" ][ 2 ] = "Guy on Girl" />

Notice here that as we add rows to the query, we are setting cell values by directly accessing the cell positions. We are treating the query as a Struct of Arrays. Notice that the first structure notation is the name of the column and the second structure notation is the row in the record set. Together we get the exact location in the query that we wish to update.

Using that knowledge, we can easily loop over a query in a CFScript blocking using an index loop.

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

  • <cfscript>
  •  
  • // Loop over the records in the query.
  • for (
  • intRow = 1 ;
  • intRow LTE qPornStars.RecordCount ;
  • intRow = (intRow + 1)
  • ){
  •  
  • // Output the name some values. When doing so, access the
  • // query as if it were a structure of arrays and we want
  • // only get values from this row index.
  • WriteOutput(
  • qPornStars[ "name" ][ intRow ] & " is a " &
  • qPornStars[ "sex" ][ intRow ] & " that prefers doing " &
  • qPornStars[ "preferred_category" ][ intRow ] & " movies." &
  • "<br />"
  • );
  • }
  •  
  • </cfscript>

This will produce the following output:

Jenna Jameson is a Female that prefers Girl on Girl movies.
Peter North is a Male that prefers Guy on Girl movies.

As you can see, it takes a little more elbow grease, but looping over a query inside of a CFScript is more than doable. And as we saw above, understanding how to access query cells without CFLoop gives you the ability to build queries as well.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



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

Reader Comments

Oct 15, 2006 at 12:50 PM // reply »
3 Comments

Thanks for the post Ben. This helped me loop through an array in cfscript.

Ryan


Oct 15, 2006 at 12:51 PM // reply »
6,516 Comments

Ryan,

Always glad to help. Yeah, when it boils down to it, there are really only two types of loops in CFScript... Index loops as you would do with an array or a query, or Collection loops as you would do with a Struct.

Drop me a line if you need help with anything else.


Jul 31, 2007 at 7:47 PM // reply »
11 Comments

What happens if Jenna comes back Girl on Girl and Girl on Guy?


Aug 1, 2007 at 7:15 AM // reply »
6,516 Comments

I don't care what happens so long as someone in the room has a video camera :D


Jan 28, 2008 at 1:27 PM // reply »
11 Comments

in cf7 at least while testing
for 46028 records,

109-125ms
<cfoutput query="qryTest">
#field1#
</cfoutput>

was faster than
172-219ms
<cfscript>
for(i=1; i lte qryGetAMFiles.recordcount; i=i+1) {
writeOutput(qryTest.field1[i] & " ");
}
</cfscript>

in 5 tests the mins/max were shown.


Feb 4, 2008 at 9:17 PM // reply »
6,516 Comments

@Steve,

I think that just goes to show you have to go with whatever you find easier / most readable. Thanks for dropping in the testing there.


Jul 6, 2008 at 11:57 AM // reply »
1 Comments

Thanks,

This is a only posting I could find that shows how to get at individual data fields from inside cfscript


Apr 17, 2009 at 5:22 PM // reply »
1 Comments

Thanks Ben... as usual, your site pulls through.


Apr 17, 2009 at 5:28 PM // reply »
6,516 Comments

@Jon,

Awesome my man, glad to help!


Jun 16, 2009 at 12:07 PM // reply »
7 Comments

Thanks Ben... as usual, your site pulls out. (get it?)


Jun 16, 2009 at 12:08 PM // reply »
6,516 Comments

@Luke,

Ha ha ha ha.


Jun 16, 2009 at 3:10 PM // reply »
11 Comments

You need to backdate our points from kinky posts :P
err, umm, our kinky points from posts.


Jun 19, 2009 at 8:00 PM // reply »
6,516 Comments

@Steve,

Yeah, at one point I just had to flip the switch. Unfortunately, I had no good way to retrofit the existing comments.


Jul 16, 2009 at 11:31 AM // reply »
1 Comments

Your example is illustrative, but using porn to illustrate it is crude. Why not use examples of alcoholics, crackheads, and child molesters to illustrate ColdFusion? Porn is disgusting.


Jul 18, 2009 at 1:49 PM // reply »
6,516 Comments

@Adam,

I don't find alcoholics, crackheads, or child molesters even remotely entertaining.


Oct 6, 2009 at 10:40 PM // reply »
1 Comments

Ben, thanks for the helpful and cool blog. You're my hero.


Nov 1, 2009 at 3:41 PM // reply »
6,516 Comments

@Grant,

Ha ha, no problem :) Glad to help.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »