ColdFusion Query Maintains Current Row Even When Passed By Reference

Posted February 28, 2007 at 6:31 PM by Ben Nadel

Tags: ColdFusion

I thought about this in the shower this morning (I take showers in the dark - it helps me think). I know that a ColdFusion query object knows what row it is in while being used in a CFLoop, that's obvious. But what happens if a ColdFusion query object is passed to another function in the middle of a CFLoop. Does it maintain the current row? After some quick testing, I can say that's a BIG 10-4, buddy!

I can't believe I never tested this before. This is something that would have been good to know. Why pass around a row index if the Query object is keeping track of it for you?!? To test this, I set up a little function that outputs the data for the current row of the passed in Query object:

  • <cffunction
  • name="OutputQueryRow"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="Outputs the current row of the passed in query.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Query"
  • type="query"
  • required="true"
  • hint="The query whose row data we are outputting."
  • />
  •  
  • <cfargument
  • name="Row"
  • type="numeric"
  • required="false"
  • default="#ARGUMENTS.Query.CurrentRow#"
  • hint="The row we are going to output (defaults to current row of the passed in query)."
  • />
  •  
  • <!--- Set up local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!---
  • Loop over each query column and output both the
  • column name and cell value (in this row only).
  • --->
  • <cfloop
  • index="LOCAL.ColumnName"
  • list="#ARGUMENTS.Query.ColumnList#"
  • delimiters=",">
  •  
  • #LOCAL.ColumnName# :
  • #ARGUMENTS.Query[ LOCAL.ColumnName ][ ARGUMENTS.Row ]#<br />
  •  
  • </cfloop>
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  •  
  • <!--- Create the test query. --->
  • <cfset qHottie = QueryNew( "" ) />
  •  
  • <!---
  • Now that we have the query, which is currently very blank,
  • we are going to add the test columns to it.
  • --->
  • <cfset QueryAddColumn(
  • qHottie,
  • "name",
  • "CF_SQL_VARCHAR",
  • ListToArray(
  • "Kit,Izzy,Laura,Samantha"
  • )
  • ) />
  •  
  • <cfset QueryAddColumn(
  • qHottie,
  • "best_asset",
  • "CF_SQL_VARCHAR",
  • ListToArray(
  • "Arms,Smile,Legs,Personality"
  • )
  • ) />
  •  
  •  
  • <!--- Loop over the hottie query. --->
  • <cfloop query="qHottie">
  •  
  • <h4>
  • Iteration #qHottie.CurrentRow#
  • </h4>
  •  
  • <p>
  • <!---
  • Output the row values. Notice that we are
  • passing in the query ONLY and not telling
  • it what row to use.
  • --->
  • <cfset OutputQueryRow( qHottie ) />
  • </p>
  •  
  • </cfloop>

You will notice that the ColdFusion user defined function (UDF) takes the query object as well as an optional row index argument. But also notice that I am NOT passing in this row index when invoking the method. Now, take a look at the row index default value; it's defaulting to the CurrentRow value of the passed in query argument. Very slick.

Running the above code gives us:

Iteration 1

BEST_ASSET : Arms
NAME : Kit

Iteration 2

BEST_ASSET : Smile
NAME : Izzy

Iteration 3

BEST_ASSET : Legs
NAME : Laura

Iteration 4

BEST_ASSET : Personality
NAME : Samantha

I love ColdFusion. It's too freakin' cool!

Now, this won't work with a CFScript / FOR loop. The FOR loop inside a script tag does not alter the ColdFusion query object - it merely accesses it by index. This will only work with a CFLoop tag that alters the state of the query object itself. Of course, using the UDF above, you could just pass in the alternate second row index argument if it is being called from within CFScript tags.



Reader Comments

Feb 28, 2007 at 10:11 PM // reply »
27 Comments

Sounds like a solution for nested query loops as well. Access the outter loop row from the inner loop with array notation using outterQuery.currentrow as row number.


Mar 1, 2007 at 7:21 AM // reply »
11,238 Comments

@Daniel,

Exactly!


May 29, 2007 at 9:29 PM // reply »
2 Comments

Hmmm (;
Yamato


Apr 8, 2008 at 8:01 AM // reply »
6 Comments

I've often wanted to work on query rows in custom tags, but today I needed to use a function. I just finished writing out <cfargument name="row"... when I found this post :)

One reason you might want to pass in the row is if you're doing odd/even logic or something that doesn't run in order (for whatever reason), then maybe you'd be better off with myFunction(query, row).

Thanks.


Apr 8, 2008 at 9:32 AM // reply »
11,238 Comments

@Adrian,

Agreed. I think that's why its a good compromise to allow an optional Row argument, but default it to the CurrentRow. Best of both worlds.


Sep 15, 2008 at 12:02 PM // reply »
1 Comments

It seems that currentRow is not being carried over if you pass a query to a function in a different object.


Sep 15, 2008 at 12:30 PM // reply »
11,238 Comments

@Shawn,

That's interesting. I wonder why that would be.


Feb 10, 2010 at 3:14 PM // reply »
8 Comments

Sweet, worked perfectly! Thanks again for doing all the dirty work, Ben :P


Feb 10, 2010 at 10:05 PM // reply »
11,238 Comments

@Jon,

Always glad to help :)


Oct 8, 2010 at 12:55 PM // reply »
1 Comments

This worked great, thanks for the info.


Oct 10, 2010 at 3:48 PM // reply »
11,238 Comments

@Gerry,

Thanks my man. And actually, your comment prompted me to try something else along these same lines. In the example, I'm using a .currentRow property; however, the current row state of the query is maintained implicitly. Take a look at this:

  • <!--- Build a query. --->
  • <cfset girls = queryNew( "" ) />
  •  
  • <!--- Add a column with some default values. --->
  • <cfset queryAddColumn(
  • girls,
  • "name",
  • "cf_sql_varchar",
  • [ "Sarah", "Tricia", "Joanna" ]
  • ) />
  •  
  •  
  • <!---
  • A function that returns the NAME of the query
  • using the implicit row.
  • --->
  • <cffunction name="getName">
  • <cfreturn arguments[ 1 ].name />
  • </cffunction>
  •  
  •  
  • <!--- Try to output each name using the proxy function. --->
  • <cfoutput>
  •  
  • <cfloop query="girls">
  •  
  • #getName( girls )#<br />
  •  
  • </cfloop>
  •  
  • </cfoutput>

When I run this, I get the following output:

Sarah
Tricia
Joanna

As you can see, not only is .currentRow maintained, so is the implicit iterator that allows us to reference column values without any row number.


Nov 17, 2010 at 11:21 AM // reply »
1 Comments

This is beautiful.

But I'm having trouble with the syntax for calling the function if it's in a external cfc.

Wouldn't it be something like
<cfset createObject("cfc.NAME").OutputQueryRow(query_name)>
????


Jan
Aug 10, 2011 at 4:41 AM // reply »
3 Comments

Great post !

I've tried this with with a query-loop in cfscript:

  •  
  • //myQuery has 2 rows. column 'id' is 1 in row 1, and 2 in row 2
  •  
  • while(myQuery.next())
  • {
  • writeOutput("<br />#myQuery.currentRow#: ");
  • outputCurrentRow(myQuery);
  • }
  •  
  • public void function outputCurrentRow(Query myQuery){
  • writeOutput(arguments.myQuery.id);
  • }

this outputs:
1: 1
2: 1

In this case the current row remained the first row of the query. The output for each row was the id of row 1.


Jan 11, 2012 at 10:59 AM // reply »
1 Comments

Thanks Ben! Great post. I've been making this process way too complicated... thank you for simplifying!



Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools