Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Looping

Cfloop is designed for looping over things ( lists, queries, collections, and arrays) and for looping until a condition is met - a conditional loop - also known as a while loop in other programming languages, but you don't need to worry about what its called in other languages.

Index Loop

Cfloop also allows us to loop a specified number of times, this is called and index loop and is the most basic and, in my experience, one of the most often used.

Example:

<cfloop from="1" to="10" index="counter" step="1" >
<cfoutput>Index:#counter#<br></cfoutput>
</cfloop>

In the above example the code between the opening and closing cfloop tags will be executed 10 times, and the output should look similar to:

Index: 1
Index: 2
Index: 3
Index: 4
Index: 5
Index: 6
Index: 7
Index: 8
Index: 9
Index: 10

From the output we can see that for each iteration though the loop the counter index variable gets incremented by 1, this is controlled by the step attribute in the cfloop tag, the default increment step is one so I could have omitted it but included it for for completeness. For example if we changed the step to 2 we would get 1,3,5,7,9 for our index.

Conditional Loop

A conditional loop, as I mentioned above, continues looping until a specified condition is met. In order to work and not get caught in an infinite loop the condition eventually must happen, so each iteration through the loop the instructions should change until the condition is false. What we typically do is set the base condition and then inside the loop we update the base condition.

Example:

<cfset x = 1>
<cfloop condition="x lte 10">
<cfoutput>X = #x#<br></cfoutput>
<cfset x = x + 1>
</cfloop>

The above example we set our base condition x=1 and then the condition of our loop specifies that the loop should continue going until x lte 10 (x less than or equal to 10). Inside the cfloop tags we update x by incrementing it by 1 AFTER we have displayed x, if we incremented x before we would have 2-11 instead of 1-10, since the increment happens before we use it.

Looping over a list

Looping over a list is very easy with the cfloop tag since it comes with a list attribute built right in.

Example:

<cfset daysOfWeek="Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday">


<cfloop list="#daysOfWeek#" index="day" delimiters=",">
<cfoutput>#day#<br></cfoutput>
</cfloop>

In the above example we set up a list of days of the week, our cfloop tag has two new attributes, the list attribute and the delimiters attribute. Of the three attributes only the list and index attributes are required as the default delimiters attribute is the comma. The delimiters attribute tells the cfloop tag how the list is separated. So we already know from the previous examples that the index attribute will hold the value of the day as the list is looped over. We notice that in the list attribute the name of the list is surrounded by #'s we need to do this because the list attribute needs to hold the actual list, which is just a variable holding a string separated with commas, if we didn't put the #'s around the list we would end up with only daysOfWeek as our output not the actual contents of the list.

Looping over a Query

Looping over a query is pretty much the same as looping over a list with the exception that we have a couple more optional loop attributes startrow and endrow. The attributes indicate which row to start and end when outputting the query contents. These attributes make it easy for you to paginate the results of query, something we will not be covering in the basic level tutorials.

Example:

<cfloop query="rsUsers" >
<cfoutput>#rsUsers.firstName# &nbsp; #rsUsers.lastName#</cfoutput>
</cfloop>

Conclusion

In this tutorial we learned that Coldfusion provides us with several different methods of looping using a single tag, the way we loop is differentiated by the combination of attributes we use with the tag. The attributes to remember for the four basic looping types are:

Index loop: from, to, index

Conditional loop: condition

List loop: list, index, delimiters

Query loop: query