<CFLOOP INDEX="parameter_name" FROM="beginning_value" TO="ending_value" STEP="increment"> ... HTML or CFML code to execute ... </CFLOOP>
An index loop repeats for a number of times determined by a range of numeric values. Index loops are commonly known as FOR loops, as in "loop FOR this range of values."
Required. Defines the parameter that is the index value. The index value will be set to the FROM value and then incremented by 1 (or the STEP value) until it equals the TO value.
Required. The beginning value of the index.
Required. The ending value of the index.
Optional. Default is 1. Sets the value by which the loop INDEX value is incremented each time the loop is processed.
In this example, the INDEX variable is incremented for each iteration of the loop. The following code loops five times, displaying the INDEX value of the loop each time:
<CFLOOP INDEX="LoopCount" FROM="1" TO="5"> The loop index is <CFOUTPUT>#LoopCount#</CFOUTPUT>.<BR> </CFLOOP>
The result of this loop in a browser looks like this:
The loop index is 1. The loop index is 2. The loop index is 3. The loop index is 4. The loop index is 5.
In this example, the STEP value has a default value of 1. But you can set the STEP value to change the way the INDEX value is incremented. The following code counts backwards from 5:
<CFLOOP INDEX="LoopCount" FROM="5" TO="1" STEP="-1"> The loop index is <CFOUTPUT>#LoopCount#</CFOUTPUT>.<BR> </CFLOOP>
The result of this loop in a browser looks like this:
The loop index is 5. The loop index is 4. The loop index is 3. The loop index is 2. The loop index is 1.