This is a basic ColdFusion tutorial on how to loop over a list. It's pretty simple once you get the gist of it, but getting started can be tricky. Another reference can be found at Adobe LiveDocs.
Basically, there are five steps:
1. Define the list
To loop over a list, you must first have a list to loop over. A list is a combination of different or same-typed values. Usually, lists are separated with commas, but you can use anything to separate lists. I usually use a bell character, but the default is a comma.
NOTE: Make sure that you do not have any spaces between your list elements.
<cfset radioList = "one,two,three" />
2. Create a CFOUTPUT block
A CFOUTPUT block consists of an open and a close CFOUTPUT tag. This tells the server to interpret anything between those two tags as ColdFusion code, so any string surrounded by pound signs (#) will be interpreted as a variable. If the variable hasn't been defined, CF will let you know it by crashing.
<cfoutput>
</cfoutput >
3. Create a CFLOOP block
You can perform a loop over many different things, including collections like arrays and structures, lists, and queries to name a few. You can also loop over stuff coming from COM objects, but that's advanced stuff.
To loop over a list, you have to define what list you're looping over, and what variable you're going to use to refer to the position in the list (here called the "index").
NOTE: Make sure that you use the variable called radioList ("#radioList#") and not just the string "radioList", because CF doesn't know that "radioList" is a variable here unless you tell it.
<cfloop list="#radioList#" index="i" >
</cfloop >
4. Do something in the loop block
Between the opening and closing CFLOOP tags, you may define what will be done with every item in the list. I'm setting a few variables in the attributes scope to zero if they don't exist, like this:
<cfparam name="attributes.#i#" default="0" / >
NOTE: Because this is inside a CFOUTPUT block, the code will render like this:
<cfparam name="attributes.one" default="0" / >
<cfparam name="attributes.two" default="0" / >
<cfparam name="attributes.three" default="0" / >
5. Show the results
One of the greatest things ever in any language, machine or human, is CFDUMP. No, it's nothing disgusting, it's actually quite beautiful. What it does is spit out a structure, no matter how simple or complex, in a human-readable format. This helps when you've got a structure that might have arrays nested in other arrays nested in structures n levels deep (ever try dumping application.fusebox in a Fusebox 4+ application?).
<cfdump var="#attributes#" label="Results" / >
Your full code should look like this:
<cfset radioList = "one,two,three" />
<cfoutput>
<cfloop list="#radioList#" index="i">
<cfparam name="attributes.#i#" default="0" />
</cfloop>
</cfoutput>
<cfdump var="#attributes#" label="Results" />
Save the page with a .cfm extension and save in a test area of your development Web server. When you browse to the .cfm page you've created, the results should look something like this:
Now, if you add this line of code at the very beginning of your existing code:
<cfset attributes.one = 1 />
And browse to the page again, your results should look something like this:
CFPARAM just sets a default value for something, so in the first dump, all default values were assigned. In the second dump, we see that attributes.one already existed, and was assigned a value of "1". Because it already existed, the value was not overwritten.
Basically, there are five steps:
- Define the list
- Create a CFOUTPUT block
- Create a CFLOOP block inside the CFOUTPUT block
- Do something in the loop block
- Show the results
1. Define the list
To loop over a list, you must first have a list to loop over. A list is a combination of different or same-typed values. Usually, lists are separated with commas, but you can use anything to separate lists. I usually use a bell character, but the default is a comma.
NOTE: Make sure that you do not have any spaces between your list elements.
2. Create a CFOUTPUT block
A CFOUTPUT block consists of an open and a close CFOUTPUT tag. This tells the server to interpret anything between those two tags as ColdFusion code, so any string surrounded by pound signs (#) will be interpreted as a variable. If the variable hasn't been defined, CF will let you know it by crashing.
<cfoutput>
3. Create a CFLOOP block
You can perform a loop over many different things, including collections like arrays and structures, lists, and queries to name a few. You can also loop over stuff coming from COM objects, but that's advanced stuff.
To loop over a list, you have to define what list you're looping over, and what variable you're going to use to refer to the position in the list (here called the "index").
NOTE: Make sure that you use the variable called radioList ("#radioList#") and not just the string "radioList", because CF doesn't know that "radioList" is a variable here unless you tell it.
4. Do something in the loop block
Between the opening and closing CFLOOP tags, you may define what will be done with every item in the list. I'm setting a few variables in the attributes scope to zero if they don't exist, like this:
NOTE: Because this is inside a CFOUTPUT block, the code will render like this:
<
<
5. Show the results
One of the greatest things ever in any language, machine or human, is CFDUMP. No, it's nothing disgusting, it's actually quite beautiful. What it does is spit out a structure, no matter how simple or complex, in a human-readable format. This helps when you've got a structure that might have arrays nested in other arrays nested in structures n levels deep (ever try dumping application.fusebox in a Fusebox 4+ application?).
Your full code should look like this:
<cfoutput>
<cfloop list="#radioList#" index="i">
<cfparam name="attributes.#i#" default="0" />
</cfloop>
</cfoutput>
<cfdump var="#attributes#" label="Results" />
Save the page with a .cfm extension and save in a test area of your development Web server. When you browse to the .cfm page you've created, the results should look something like this:
And browse to the page again, your results should look something like this:
CFPARAM just sets a default value for something, so in the first dump, all default values were assigned. In the second dump, we see that attributes.one already existed, and was assigned a value of "1". Because it already existed, the value was not overwritten.
Comments