rss
twitter
    Made it to the hotel. Where is everyone?

ColdFusion 10 - For-In Loop for Queries

On top of some of the 'big ticket' items in ColdFusion 10, there are also a lot of small langauge enhancements that will make it even easier to use ColdFusion. One such example is using for-in syntax in queries.

Here is a basic example:

view plain print about
1<cfscript>
2    books = new query(datasource="cfbookclub", sql="select * from Books").execute();
3     for( book in books.getResult() ){
4         writeOutput( "<strong>Title:</strong> " & book.title);
5         writeOutput(" <strong>Description:</strong> " & book.bookdescription & "<hr/>");
6     }
7     writeDump( book )
8
</cfscript>

Here is the result of the dump from the code above.

Query 'for-in' example

Notice that, for each iteration, the value of book (or whatever your variable is named) is a structure where each of the keys is a column name from our query. This actually makes a lot of sense since a ColdFusion query object is nothing more than a specialized array of structures anyway.

As well as some of the bigger additions, I plan on blogging more about language enhancements such as this as they can get easily overlooked.

3 comments

(Comment Moderation is enabled. Your comment will not appear until approved.)
Dan Vega said...
Thanks for the posts Scott, keep them coming! I think something else to take away from this is consistency. Now whenever you are working with any complex object (Array,Struct,Query,Obj) you can use the for-in-loop to iterate over the elements.
Scott Stroz said...
That is a great point Dan.
Phillip Senn said...
Wow. It's like the change from cf8 syntax to cf9 and 10 is so radically different that they are two languages now.