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

The first thing that we need to do it to query a database

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



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



Example
The following example increments CountVar from 1 to 5.



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




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:



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



Example



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



Example

This loop displays four names:


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:



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:



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



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




More Information visit ColdFusion Development.


0 comments