Index and Item in Loops
One of the strange anomilies in the CFML language is that of the attributes of the <cfloop> tag. For example, if you loop an array in a cfloop tag, and you have to use the index attribute. But what IS in the index attribute?, let’s have a look at the sample below:
<cfscript>
myArray = ListToArray("one,two,three,four,five");
</cfscript>
<cfloop array="#myArray#" index="i">
<cfdump var="#i#">
</cfloop>

You might wonder what is the deal? But by the name itself, index means the numerical index of the items in the loop, what we are actually getting is the item!
Railo 4 solves this in a simple and elegant way, to retain backwards compatibility. If you just use index things will work as normal, but if you ALSO add the item attribute, you get the results that you would have expected!:
<cfscript>
myArray = ListToArray("one,two,three,four,five");
</cfscript>
<cfloop array="#myArray#" index="i" item="it">
<cfdump var="#i#" label="The numerical index">
<cfdump var="#it#" label="The item in the array">
</cfloop>

This goes for Structures too!
<cfscript>
myStruct = {one: "The One" ,two: "The Two",three: "The Three",four:"The Four",five: "The Five"};
</cfscript>
<cfloop collection="#myStruct#" index="i" item="it">
<cfdump var="#i#" label="The numerical index">
<cfdump var="#it#" label="The item in the array">
</cfloop>

FOR/IN LOOPS
Since we have expanded the scripting capabilities in Railo 4, you can now do a host of loops just using the for(x in y){} syntax. We have also changed how we loop over queries, now giving you access to the row iteslf!
<cfscript>
myStruct = {one: "The One" ,two: "The Two",three: "The Three",four:"The Four",five: "The Five"}; myArray = ListToArray("one,two,three,four,five"); //New Query Functions myQuery = Query("id":[1,2,3,4,5],value:[1,2,3,4,5]); for(key in myStruct){ dump(key); } WriteOutput("<br>"); for(item in myArray){ dump(item); } WriteOutput("<br>"); for(row in myQuery){ dump(myQuery.currentRow); dump(myQuery.columnList); dump(row); } WriteOutput("<br>"); </cfscript>
CFLOOP – group
Up till Railo 3.3 the attribute group was only available for the tag CFOUTPUT. It is an attribute that mimics the group by behaviour of SQL with queries. Now in Railo 4.0 the group attribute is also available for the CFLOOP tag.
For the group attribute to work, the corresponding recordsets need to be sorted by the column to group by. If you use the group attribute you can loop over the grouped records by using a subsequent CFLOOP tag.
Let’s have a look at the following example:
<cfset customers = query( "id":[1,2,3,4,5], "name":["Stefan","Michael","Mark","Tanja","Steffi"], "lastname":["Rubner","Offner","Drew","Stadelmann","Heise"], "gender":["male","male","male","female","female"]) /> <cfoutput> You have #customers.recordCount# customers!<br /> <cfloop query="customers" group="gender"> #customers.gender#: <br /> <cfloop> - # customers.name# #customers.lastname#<br /> </cfloop> </cfloop> </cfoutput>
The first CFLOOP above uses the group attribute. This instructs Railo to group adjacent rows together with the same group column value. Inside this CFLOOP tag, there is another CFLOOP tag that loops over the rows of the same group value. The output of the above code will be:
You have 5 customers! female: - Tanja Stadelmann - Steffi Heise male: - Stefan Rubner - Michael Offner - Mark Drew
You can see that the rows with the same gender value are all gathered into one and the same group. Output grouping is way easier like this.