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 (4) [RSS]
Blog Design (1) [RSS]
CFProject Scripts (2) [RSS]
Coldfusion Charts (3) [RSS]
Coldfusion Functions (5) [RSS]
Coldfusion Overview (1) [RSS]
Coldfusion Tutorials (16) [RSS]
For Sale (2) [RSS]
Image Manipulation (1) [RSS]
Java (1) [RSS]
JavaScript (4) [RSS]
PHP (1) [RSS]
Railo (4) [RSS]
SQL (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Simple Coldfusion Upload Script
Alban said: Here is the error I get on the cffile line: String index out of range: -1 [More]

Coldfusion 8 Functions
Mark said: Can Cold Fusion do math functions similar to say Excel? I need to make several calculations, and if ... [More]

BlogCFC on Railo
iphone clone said: Thanks for sharing [More]

Coldfusion Protx VSP Form Integration Kit
confinedspace said: @Dom: Cheers for that - helped me out. Just one thing to note, I would add "all" as a thir... [More]

Coldfusion to PHP?
Jonny Shaw said: PHP is very similar to most other OOP languages, which i am quite familiar with, so it wasn't really... [More]

Recent Entries

No recent entries.
ColdFusion Blog | ColdFusion Hosting | ColdFusion Q & A