Loop Statements

In ColdFusion, loops are also quite a bit different from other languages in that their arguments look more like html attributes. However, I must first start out that telling you ColdFusion loops are one of my favorite things about ColdFusion. You can literally loop through anything in about any way that you want. You can loop through query results by just giving the loop the query name and a variable to increment. Alternatively, you can simply loop within ranges by giving ColdFusion the increment variable, starting point, ending point, and an index. Granted it isn’t as cool as ColdFusion being able to query a query (we will get to this), but the flexibility of loops give ColdFusion a step up over other languages.

    <cfloop index="x" from="1" to="10" step="1">
        #x# <br/>

The index argument is the variable you want to be involved in the loop. Usually, we will increment this variable by the step argument until we have ColdFusion return all the results that we wanted. from is your beginning value of x, and to is the finally result of x. The last argument below is the step, which is by how much do we want to increment the index.

Looping Query Results

Looping query results requires that you already know how to use ColdFusion to get results from an SQL server. What you need to do is to run a query first. Then that query will be an object that you can loop through. You just need to provide the query name and then you need some way to use each iteration to output a different record of the query. You can simply use an incrementing variable that can be defined in the initial loop statement. It is best to discuss this when we have an actual query to run.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Share on StumbleUpon
Digg this
Tweet about this on Twitter
Share on Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.