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

History

New in ColdFusion MX: On Windows, if the cfdirectory tag action = "list", the cfdirectory tag does not return the directory entries "." (dot) or ".." (double dot), which represent "the current directory" and "the parent directory." (In earlier releases, ColdFusion returned all the entries.)

CFML code such as the following, which was acceptable in ColdFusion 5, may cause incorrect output in ColdFusion MX:

<cfdirectory action = "list" directory ="c:\" name="somename">
Files in c:\<br>
<cfloop query = "somename" startrow = 3>
  #name#<br> >

For more information, see "cfdirectory".

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

This example shows a cfloop looping over a query the same way as a cfoutput tag that uses the query attribute:

<cfquery name = "MessageRecords" 
  dataSource = "cfsnippets"> 
    SELECT * FROMMessages 
</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 10th through the 20th record returned by MyQuery:

<cfset Start = 10> 
<cfset End = 20> 
<cfloop query = "MyQuery" 
  startRow = "#Start#" 
  endRow = "#End#"> 
  <cfoutput>#MyQuery.MyColName#</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 advantage of looping over a query is that you can use CFML tags that are not allowed in a cfoutput tag. 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 = "TemplateName"> 
  <cfinclude template = "#TemplateName#"> 
</cfloop>

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

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


MarkZet said on Dec 14, 2004 at 7:23 AM :
If you have an inner and outer loop over two queries, expressions referring to data in the outer query will return data from the first record during the execution of the inner query loop. However, the CurrentRow pointer still works as expected.

Example: suppose you have query 'q1' (a column named 'letter' containing two records with values 'A' and 'B') and query 'q2' (a column named 'number' containing two records with values 1 and 2) :
<CFLOOP query="q1">
<CFLOOP query="q2">
#q1.letter# #q2.number#
</CFLOOP>
</CFLOOP>
This will produce the following output:
A 1 A 2 A 1 A 2

Workaround:
<CFLOOP query="q1">
<CFLOOP query="q2">
#q1.letter[q1.CurrentRow]# #q2.number#
</CFLOOP>
</CFLOOP>
Gives:
A 1 A 2 B 1 B 2

Question: is this by design? It has caused some unexpected output at occasions. Especially in a setup such as the following:
<CFLOOP query="q1"><CFINCLUDE template="show.cfm"></CFLOOP>
and in template show.cfm:
<CFLOOP query="q2">#q1.letter#: #q2.number#</CFLOOP>
There's really no way to be sure but to postfix every query inside a <CFLOOP query=""> with [q.CurrentRow].
fdgsogc91 said on Dec 17, 2004 at 7:59 AM :
Thank you MarkZet for your comment/correction. I was trying to mark a list of check boxes as checked where the userid had previously selected. In essence showing the user thepreferences by checkbox that they selected in an earlier session.

I could not get this to work properly until I entered your [queryname.CurrentRow] code. Thank you, thank you, thank you.

 

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

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt175.htm