<cfloop>

Looping is a very powerful programming technique that lets you repeat a set of instructions or display output repeatedly until one or more conditions are met. cfloop supports five types of loops.

Usage

<cfloop
    [condition=string]
    [item=string]
    [query=object]
    [charset=string]
    [struct=struct]
    [times=numeric]
    [delimiters=string]
    [list=string]
    [startline=number]
    [to=number]
    [step=number]
    [collection=any]
    [endrow=number]
    [startrow=number]
    [index=string]
    [file=string]
    [maxrows=number]
    [groupcasesensitive=boolean]
    [from=number]
    [endline=number]
    [label=string]
    [characters=number]
    [array=array]
    [group=string]
><!--- body ---></cfloop>

This tag must have a body.

This tag is also supported within cfscript

Attributes

Attribute Description
condition
(string, optional)

Condition that controls the loop.

item
(string, optional)

Key for the collection

query
(object, optional)

Query that controls the loop. this can be a variable name or the query itself.

charset
(string, optional)

charset for read the file

struct
(struct, optional)

struct to loop over

times
(numeric, optional)

used for a simple loop that is looping n times

delimiters
(string, optional)

Character(s) that separates items in list

list
(string, optional)

A list, variable, or file name; contains a list

startline
(number, optional)

start line

to
(number, optional)

Ending value of index.

step
(number, optional)

Step by which to increment or decrement the index value.

collection
(any, optional)

Collection to loop over

endrow
(number, optional)

Last row of query that is included in the loop. You cannot use this attribute together with the attribute maxRows.

startrow
(number, optional)

First row of query that is included in the loop.

index
(string, optional)

Index value. Lucee sets it to from value and increments or decrements by step value, until it equals to value.

file
(string, optional)

file path

maxrows
(number, optional)

Specifies the maximum number of rows to display in the output section. You cannot use this attribute together with the attribute endrow.

groupcasesensitive
(boolean, optional)

Boolean indicating whether to group with regard to case or not. The default value is NO; case is considered while grouping. If the query attribute specifies a query object that was generated by a case-insensitive SQL query, set the groupCaseSensitive attribute to NO to keep the recordset intact.

from
(number, optional)

Beginning value of index.

endline
(number, optional)

end line

label
(string, optional)

used to a address this loop from a "break" or "continue" statement (instead of the nearest one).

characters
(number, optional)

The number of characters to read during each iteration of the loop from the file specified in the file attribute. If the value of the characters attribute is more than the number of characters in the file, Lucee uses the number of characters in the file.

array
(array, optional)

An array.

group
(string, optional)

Specifies the query column to use when you group sets of records together to send as an e-mail message. For example, if you send a set of billing statements to customers, you might group on "Customer_ID." The group attribute, which is case sensitive, eliminates adjacent duplicates when the data is sorted by the specified field. See the Usage section for exceptions.

Examples

<cfquery datasource="UserDB" name='qUser'>
	Select Username, FirstName, LastName
	From UserDB.User
</cfquery>

<cfloop query='qUser'>
	<cfoutput>
		#Username# - #FirstName# - #LastName# <br/>
	</cfoutput>
</cfloop>

See also

  • tag-break