|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Best Practice: Looping over a query in cfscript?
So yesterday I was working in cfscript and needed to loop over a query. IAndy Matthews 11/20/08 09:43 A 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?Jason Fisher 11/20/08 10:31 A You're right...with cfscript though, you can't easily loop over the query,Andy Matthews 11/20/08 11:34 A Here is how I loop over a query in cfscript. Quick, simple and easy.Chuck 11/20/08 08:00 P Hm, I'd think I'd do that loop the same way with cfloop - there is noDominic Watson 11/20/08 10:31 A NICE!!!!Andy Matthews 11/20/08 11:35 A 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 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? 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? 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> 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 ----- 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 -----
|
May 24, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||