House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Best Practice: Looping over a query in cfscript?

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
11/20/2008 09:43 AM

So yesterday I was working in cfscript and needed to loop over a query. I reviewed my options and decided for elegance sake that a for / in loop was choice. Only problem is that for / in loops in cfscript can't be used with query objects. After trying a few things, I fell back and punted with the following code: qGetTheme = siteGW.GetSiteThemeValuesByThemeID(ARGUMENTS.themeID); colList = qGetTheme.columnlist; for ( item=1;item <= ListLen(colList);item++ ) {             SiteInfo.theme[ListGetAt(colList,item)] = qGetTheme[ListGetAt(colList,item)][1]; } I know this works, and is perfectly legit, but I'd love to see if there's a better way of doing this in cfscript. Anyone? Andy

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jason Fisher
11/20/2008 10:31 AM

This isn't looping over the query, though, right?  It's looping over the columns in the first row only. Looks like the SiteInfo.theme struct is simply a mirror of the first row of the query when all is said and done.  Is that what you're going for?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
11/20/2008 11:34 AM

You're right...with cfscript though, you can't easily loop over the query, at least not in what I'd consider an elegant way. This isn't looping over the query, though, right?  It's looping over the columns in the first row only. Looks like the SiteInfo.theme struct is simply a mirror of the first row of the query when all is said and done.  Is that what you're going for?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Chuck
11/20/2008 08:00 PM

Here is how I loop over a query in cfscript. Quick, simple and easy. <cfscript>   // Create Query   myQry = QueryNew("ColumnA,ColumnB,ColumnC","VarChar,VarChar,VarChar");   // Load Query With Data   for (i=1; i LTE 10; i=i+1){     QueryAddRow(myQry, 1);     QuerySetCell(myQry, "ColumnA", RandRange(10000, 20000), i);     QuerySetCell(myQry, "ColumnB", RandRange(10000, 20000), i);     QuerySetCell(myQry, "ColumnC", RandRange(10000, 20000), i);   } </cfscript> <!--- Dump the Query to see what is in the recordset ---> <cfdump var="#myQry#"> <cfscript>   // Loop Over Query and write out the data   for (q=1; q LTE myQry.RecordCount; q=q+1){     writeoutput(myQry.ColumnA[q] & " -- ");     writeoutput(myQry.ColumnB[q] & " -- ");     writeoutput(myQry.ColumnC[q] & "<br />");   } </cfscript>

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dominic Watson
11/20/2008 10:31 AM

Hm, I'd think I'd do that loop the same way with cfloop - there is no cfloop shortcut for looping over the query columns. What you have there is fine though I would personally use an array instead of a list in the loop as it is generally more efficient and easier to read in my opinion: qGetTheme = siteGW.GetSiteThemeValuesByThemeID(ARGUMENTS.themeID); cols = ListToArray( qGetTheme.columnlist ); nCols = ArrayLen(cols); for ( item=1;item <= nCols; item++ ) {     SiteInfo.theme[ cols[item] ] = qGetTheme[ cols[item] ][1]; } Looping over the rows of the query would be slightly different however. Here is a neat but undocumented way to loop a query in cfscript: <cfscript>   foo = QueryNew('');   bar = [1,2,3,4];   QueryAddColumn(foo,'bar','integer',bar);      while(foo.next()){     writeoutput(foo.bar[foo.getCurrentRow()]);   } </cfscript> Output would be: 1234 HTH Dominic ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
11/20/2008 11:35 AM

NICE!!!! That's the sort of thing I'm looking for Dominic! Thanks! Hm, I'd think I'd do that loop the same way with cfloop - there is no cfloop shortcut for looping over the query columns. What you have there is fine though I would personally use an array instead of a list in the loop as it is generally more efficient and easier to read in my opinion: qGetTheme = siteGW.GetSiteThemeValuesByThemeID(ARGUMENTS.themeID); cols = ListToArray( qGetTheme.columnlist ); nCols = ArrayLen(cols); for ( item=1;item <= nCols; item++ ) {     SiteInfo.theme[ cols[item] ] = qGetTheme[ cols[item] ][1]; } Looping over the rows of the query would be slightly different however. Here is a neat but undocumented way to loop a query in cfscript: <cfscript>   foo = QueryNew('');   bar = [1,2,3,4];   QueryAddColumn(foo,'bar','integer',bar);      while(foo.next()){     writeoutput(foo.bar[foo.getCurrentRow()]);   } </cfscript> Output would be: 1234 HTH Dominic ----- Excess quoted text cut - see Original Post for more ----- a ----- Excess quoted text cut - see Original Post for more -----


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

May 24, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
     1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31     

Designer, Developer and mobile workflow conference