Chapter 1: ColdFusion Tags
<CFSET CountVar=CountVar + 1>
The loop index is <CFOUTPUT>#CountVar#</CFOUTPUT>.<BR>
</CFLOOP>
The result of this loop in a browser would look something like:
The loop index is 1.
The loop index is 2.
The loop index is 3.
The loop index is 4.
The loop index is 5.
Looping over a Query
A loop over a query repeats for every record in the query record set. The CFLOOP
results are just like a CFOUTPUT. During each iteration of the loop, the columns of the
current row will be available for output. CFLOOP allows you to loop over tags that can
not be used inside CFOUTPUT.
Syntax
<CFLOOP QUERY="query_name"
STARTROW="row_num"
ENDROW="row_num">
QUERY
Required. Specifies the query that will control the loop.
STARTROW
Optional. Specifies the first row of the query that will be included in the loop.
ENDROW
Optional. Specifies the last row of the query that will be included in the loop.
Example 1
The following example shows a CFLOOP looping over a query that works in the same
way as a CFOUTPUT tag using the QUERY attribute:
<CFQUERY NAME="MessageRecords"
DATASOURCE="cfsnippets">
SELECT * FROM Messages
</CFQUERY>
<CFLOOP QUERY="MessageRecords">
<CFOUTPUT>#Message_ID#</CFOUTPUT><BR>
</CFLOOP>
Example 2
CFLOOP also provides iteration over a recordset with dynamic starting and stopping
points. Thus you can begin at the tenth row in a query and end at the twentieth. This
mechanism provides a simple means to get the next n sets of records from a query.
131
Need help?
Do you have a question about the COLDFUSION 4.5-CFML LANGUAGE and is the answer not in the manual?
Questions and answers