Looping in Coldfusion

Looping is a programming technique that repeats a set of instructions or displays output repeatedly until one or more conditions are met.

This tag supports the following types of loops:

  • cfloop: index loop
  • cfloop: conditional loop
  • cfloop: looping over a date or time range
  • cfloop: looping over a query
  • cfloop: looping over a list, a file, or an array
cfloop: index loop

An index loop repeats for a number of times that is determined by a numeric value. An index loop is also known as a FOR loop.

Syntax

<cfloop

index = "parameter name"

from = "beginning value"

to = "ending value"

step = "increment">

HTML or CFML code ...

</cfloop>

Example

The loop index is #LoopCount#.

cfloop: conditional loop

A conditional loop iterates over a set of instructions as long as a condition is True. To use this type of loop correctly, the instructions must change the condition every time the loop iterates, until the condition is False. Conditional loops are known as WHILE loops, as in, "loop WHILE this condition is true."

Syntax

<cfloop

condition = "expression">

...

</cfloop>

Example

The following example increments CountVar from 1 to 5.

<!--- Set the variable CountVar to 0. --->

<cfset CountVar = 0>

<!--- Loop until CountVar = 5. --->

<cfloop condition = "CountVar LESS THAN OR EQUAL TO 5">

<cfset CountVar = CountVar + 1>

The loop index is <cfoutput>#CountVar#</cfoutput>.<br />

</cfloop>
cfloop: looping over a date or time range

Loops over the date or time range specified by the from and to attributes. By default, the step is 1 day, but you can change the step by creating a timespan. The cfloop tag loops over tags that cannot be used within a cfoutput tag.

Syntax

<cfloop

from = "start time"

to = "end time"

index = "current value"

step = "increment">

</cfloop>

Example

The following example loops from today's date to today's date plus 30 days, stepping by 7 days at a time and displaying the date:

<cfset startDate = Now()>

<cfset endDate = Now() + 30>

<cfloop from="#startDate#" to="#endDate#" index="i" step="#CreateTimeSpan(7,0,0,0)#">

<cfoutput>#dateformat(i, "mm/dd/yyyy")#<br /></cfoutput>

</cfloop>

cfloop: looping over a query

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 number"

endRow = "row number">

</cfloop>

Example

<cfquery name = "MessageRecords" dataSource = "cfdocexamples">

SELECT * FROM Messages

</cfquery>

<cfloop query = "MessageRecords">

<cfoutput>#Message_ID#</cfoutput><br />

</cfloop>

cfloop: looping over a list, a file, or an array

Looping over a list steps through elements contained in any of these entities:

A variable

A value that is returned from an expression

An array

A file

Looping over a file does not open the entire file in memory.

Syntax

<cfloop

index = "index name"

array = "array"

characters = "number of characters"

delimiters = "item delimiter"

file = "absolute path and filename">

list = "list items"

...

Example

This loop displays four names:

<cfloop index = "ListElement" list = "John,Paul,George,Ringo">

<cfoutput>#ListElement#</cfoutput><br />

</cfloop>

You can put more than one character in the delimiters attribute, in any order. For example, this loop processes commas, colons, and slashes as list delimiters:

<cfloop index = "ListElement" list = "John/Paul,George::Ringo" delimiters = ",:/">

<cfoutput>#ListElement#</cfoutput><br />

</cfloop>

ColdFusion skips the second and subsequent consecutive delimiters between list elements. Thus, in the example, the two colons between "George" and "Ringo" are processed as one delimiter.

To loop over each line of a file, use the tag as follows:

<cfloop file="c:   empsimplefile.txt" index="line">

<cfoutput>#line#</cfoutput><br />

</cfloop>

To read a specified number of characters from a text file during each iteration of the loop, use the tag as follows:

<cfloop file="c:/tempsimplefile.txt" index="chars" characters="12">

<cfoutput>#chars#</cfoutput><br />

</cfloop>

To loop over an array, you can do the following:

<cfset x = ["mars","earth", "venus", "jupiter"]>

<cfloop array="#x#" index="name">

<cfoutput>#name#</cfoutput>

</cfloop>

Related Blog Entries

Comments

Archives By Subject

Advertising (3) [RSS]
Blog Design (1) [RSS]
CFProject Scripts (2) [RSS]
Coldfusion Charts (3) [RSS]
Coldfusion Functions (5) [RSS]
Coldfusion Overview (1) [RSS]
Coldfusion Tutorials (17) [RSS]
For Sale (2) [RSS]
Image Manipulation (1) [RSS]
JavaScript (4) [RSS]
PHP (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Coldfusion to PHP?
psikoteknik said: Very nice blog.Thanks for the valuable contributions to this site. Have good works. Thanks for your... [More]

Coldfusion Hosting From £2.50
Jonny Shaw said: Yes sandboxing is enabled, and no tags are disabled. I am aware of the issure with IE7, stupid IE an... [More]

Coldfusion Hosting From £2.50
Gary F said: Sounds good. Do you use sandboxing and are any tags disabled? Your jassoft.co.uk home page doesn't r... [More]

Coldfusion to PHP?
Rob Wilkerson said: You won't have any problems as long as you keep your mind open. I made the same move about 2 years a... [More]

Coldfusion to PHP?
Jonny Shaw said: The framework i will be using is Zend, Has anyone used this? [More]

Recent Entries

Coldfusion Hosting From £2.50
cf8.co.uk For Sale
Coldfusion Blog | Coldfusion Hosting | About Me