View comments | RSS feed

cfloop: looping over a query

Description

A loop over a query executes for each record in a query record set. The results are similar to those of the cfoutput tag. During each iteration, the columns of the current row are available for output. The cfloop tag loops over tags that cannot be used within a cfoutput tag.

Syntax

<cfloop 
query = "query_name"
startRow = "row_num"
endRow = "row_num">
</cfloop>

See also

cfabort, cfbreak, cfexecute, cfexit, cfif, cflocation, cfoutput, cfswitch, cfthrow, cftry; For more information, see cfloop and cfbreak in Elements of CFML in ColdFusion MX Developer's Guide

Attributes

Attribute Req/Opt Default Description

query

Required

 

Query that controls the loop.

startRow

Optional

 

First row of query that is included in the loop.

endRow

Optional

 

Last row of query that is included in the loop.

Example

<cfquery name = "MessageRecords" 
dataSource = "cfdocexamples"> 
SELECT * FROM Messages 
</cfquery>
<cfloop query = "MessageRecords"> 
<cfoutput>#Message_ID#</cfoutput><br>
</cfloop>

The cfloop tag also iterates over a record set with dynamic start and stop points. This gets the next n sets of records from a query. This example loops from the fifth through the tenth record returned by the MessageRecords query:

<cfset Start = 5> 
<cfset End = 10> 
<cfloop query = "MessageRecords" 
startRow = "#Start#" 
endRow = "#End#"> 
<cfoutput>#MessageRecords.Message_ID#</cfoutput><br>
</cfloop>

The loop stops when there are no more records, or when the current record index is greater than the value of the endRow attribute.

The following example combines the pages that are returned by a query of a list of page names into one document, using the cfinclude tag:

<cfquery name = "GetTemplate" 
dataSource = "Library"
maxRows = "5"> 
SELECT TemplateName 
FROM Templates 
</cfquery> 
<cfloop query = "GetTemplate"> 
<cfinclude template = "#TemplateName#"> 
</cfloop> 

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


refriedChicken said on Aug 15, 2005 at 1:51 PM :
There is an interesting effect in cfloop. When looping over a query returned by a cfinvoke the value does not increment when prefaced with the query name. Remove the "queryname." and it works fine. This only happens when invoking another component. When outputting it is fine. Not sure if this is a bug or just lacks documentation.
aWebGuy said on Sep 10, 2007 at 6:23 AM :
When working with cfloops nested within other cfloops, the inner loop occasionally uses the index of the outer loop which results in erroneous output. To ensure proper output, one should use the queries CurrentRow properties when referencing the values.

<cfquery name = "StudentsQry" dataSource = "myDSN">
SELECT sid, name FROM Students
</cfquery>

<cfloop query = "StudentsQry">

<h1># StudentsQry.name[StudentsQry.CurrentRow]#<h1>

<cfquery name = "CoursesQry" dataSource = "myDSN">
SELECT cid, course FROM StudentCourses WHERE sid = StudentsQry.sid[StudentsQry.CurrentRow]
</cfquery>

<cfloop query = " CoursesQry ">
# CoursesQry.course[CoursesQry.CurrentRow]#<br />
</cfloop>

</cfloop>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000294.htm