Skip to Main Content
Programming ColdFusion MX, 2nd Edition
book

Programming ColdFusion MX, 2nd Edition

by Rob Brooks-Bilson
August 2003
Intermediate to advanced content levelIntermediate to advanced
1140 pages
68h 45m
English
O'Reilly Media, Inc.
Content preview from Programming ColdFusion MX, 2nd Edition

Looping

Looping allows you to repeat specific blocks of code (both HTML and CFML) within your CFML templates. ColdFusion supports a variety of looping constructs with the <cfloop> tag, including index (for) loops, conditional (while) loops, collection loops, list loops, and query loops. For now, we are just going to cover basic index and conditional loops. Query loops are covered in Chapter 4, while collection and list loops are covered in Chapter 6.

Index Loops

Also known as a for loop, an index loop repeats a number of times specified as a range of values:

<cfloop index="index_name" from="number" to="number" step="increment">
   HTML and CFML...
</cfloop>

The index attribute of the loop specifies a variable name to hold the value corresponding to the current iteration of the loop. The from attribute initializes the starting value for the loop. The to attribute refers to the value at which iteration should stop. step specifies the increment value for each iteration of the loop. step may be either a positive or a negative number. Here is an example that uses an index loop to output all the numbers between 10 and 100 in increments of 10, with each number on its own line:

<h2>Calling the loop...</h2>
   
<cfoutput>
<cfloop index="i" from="10" to="100" step="10">
  #i#<br>
</cfloop>
</cfoutput>
   
<h2>We are now outside of the loop</h2>

Here, index is set to i. Since we want to begin the count at 10, we assign that value to the from attribute. The to attribute is set to 100 because that is where ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming ColdFusion

Programming ColdFusion

Rob Brooks-Bilson

Publisher Resources

ISBN: 0596003803Supplemental ContentErrata Page