Discussion:
count backwards with cfloop
(too old to reply)
ionic77
2005-06-17 14:59:22 UTC
Permalink
hey everyone, i'm trying to list some bits of information by year, starting
with the current year, and need to do a <cfloop> to do this.

when i start the list like this: <cfloop from="2005" to="1983" index="i"> it
doesn't return any records, but when i reverse it and make it: <cfloop
from="1983" to="2005" index="i"> it works just fine, but puts things in the
wrong order.

is there a way have a loop countbackwards?

my workaround for this is to run the loop from a variable ie:

<cfset years = '2005,2004,2003'>

<cfloop list="#years#" index="i">

is there a better way to do this?

thanks for your help :)
mxstu
2005-06-17 15:06:44 UTC
Permalink
The default for most loops is usually increment the counter by +1. Try adding step="-1" to the cfloop.
Stressed_Simon
2005-06-17 15:06:21 UTC
Permalink
Do this:-

<cfloop from="2005" to="1983" index="i" step="-1">
ionic77
2005-06-17 17:09:10 UTC
Permalink
thanks for the quick reply! it worked like a charm :)

Loading...