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




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,371 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.


Brian
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,371 Comments

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


Steve
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,371 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


Jon Clark
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,371 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,371 Comments

@Luke,

Ha ha ha ha.


Steve
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,371 Comments

@Steve,

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


Adam Smith
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,371 Comments

@Adam,

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


Grant Smith
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,371 Comments

@Grant,

Ha ha, no problem :) Glad to help.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »