Skip to main content

ColdFusion Tutorial - Looping over a list

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
  2. Create a CFOUTPUT block
  3. Create a CFLOOP block inside the CFOUTPUT block
  4. Do something in the loop block
  5. Show the results
This tutorial will show you step-by-step what to do.

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.

Comments

Popular posts from this blog

Boston Housing Dataset Missing From UC Irvine's Site

I'm putting together a series of blog posts on Python for R programmers, and I figured I'd use the Boston dataset of Boston housing prices.  It's a pretty well-known dataset for regression, and it's included in R in the MASS package and in Python in sklearn.datasets .  I know the data originally came from UCI, so I wanted to give credit where credit was due. When I clicked the first few Google links that appeared, I got this message on the UCI site: I'm sorry, the dataset "housing" does not appear to exist. Weird that they're not linked at all, but I found these links. Here's the link to the dataset in their archive. Here's the link to the data dictionary. Enjoy!

I'm Just Here for the Carnage - RoboGames 2007

So here I am in San Francisco, right on the water at Fort Mason for one of the "top geek events" in the U.S. (as proclaimed by Wired magazine).  Let me tell you, if you've never had a cocktail mixed by a robot, you haven't lived. Last night, some dudes brought in a serious robot - a biotech machine - that allowed them to make perfectly-layered B-52 shots.  Each layer was measured out in picalitres. Today, Roboexotica's Cockbot 1 is mixing and serving daquiris, Campari/oranges, and screwdrivers. This is actually Cockbot 1. I can't wait for tonight, when a flamethrowing 151 and Kahlua bot will be shooting a yard's worth of fire to ignite highly-flammible liquor. Stay tuned...