Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem displaying "reverse loop"

Status
Not open for further replies.

CFdutch

Programmer
Nov 12, 2003
49
0
0
NL
Hi I need some help with some reverse looping.

I receive a number as a variable. And from that point, I need to loop to 1. So if I receive 4, i need to ouput this

Code:
<select name="amount">
   <option value="4">4</option>
   <option value="3">3</option>
   <option value="2">2</option>
   <option value="1">1</option>
</select>

I tried with a loop, but I can't output a "reverse loop".
It should be pretty easy, but the problem is I'm working to a deadline and I'm sitting from 07:00 AM local(AMS) YESTERDAY :-s

So the best solution would be sleep, but the deadline is at the end of the day and I need some little things done like this one.

Please help me :D, thanks in advance and a meery x-mass and a happy 2005!!!!!

Some people can learn, some people can teach.
 
Here's an example copied straight from the CF Help files:
Code:
<cfloop index = "LoopCount" 
  from = "5" 
  to = "1" 
  step = "-1">
The loop index is <cfoutput>#LoopCount#</cfoutput>.<br>
</cfloop>
The output of this loop is as follows: 

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.
Notice the "From" and "To" attributes are backwards, and that the "Step" attribute is set to "-1", this makes the loop cound backwards.



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
On that note even though documentation says "step" is optional, which it is because the default is 1. it is required in an instance like this.

Code:
<cfloop from = "4" to = "1" index = "i">
<cfoutput>#i#</cfoutput>
</cfloop>

will not work even with the from starting at 4 and the to is 1. Step IS required at this point to count backwords.

Its also a happy thing to use when you want to only count even or odd numbers. There is a lot you could do with it, not just even or odd numbers.
Code:
<cfloop from = "2" to = "10" index = "i" [b]step="2"[/b]>
<cfoutput>#i#</cfoutput>
</cfloop>

will produce 2 4 6 8 10

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Thanks! That was just what I needed. I forgot the -1 step ;)

Merry christmass to you all!

Some people can learn, some people can teach.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top