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 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »