The cfloop
collection
attribute lets you loop over a structure or a COM/DCOM collection object:
Each collection item is referenced in the cfloop
by the variable name that you supply in the item
attribute. This type of an iteration is generally used to access every object within a COM/DCOM collection, or every element in the structure. The loop is executed until all objects have been accessed.
The collection
attribute is used with the item
attribute in a cfloop
. In the example that follows, item
is assigned a variable called
file2
,
so that with each cycle in the cfloop, each item in the collection is referenced. In the cfoutput section, the name property of the file2 item is referenced for display.
This example employs a COM object to output a list of files. In this example, FFUNC
is a collection of file2
objects.
<cfobject class = FileFunctions.files
name = FFunc
action = Create>
<cfset FFunc.Path = "c:\">
<cfset FFunc.Mask = "*.*" >
<cfset FFunc.attributes = 16 >
<cfset x = FFunc.GetFileList()>
<cfloop collection = #FFUNC
# item = "file2">
<cfoutput>
#file2.name# <BR>
</cfoutput>
</cfloop>
This example loops through a structure (used as an associative array):
...<!--- Create a structure and loop through its contents --->
<cfset Departments = StructNew()> <cfset val = StructInsert(Departments, "John ", "Sales ")> <cfset val = StructInsert(Departments, "Tom ", "Finance ")> <cfset val = StructInsert(Departments, "Mike ", "Education ")> <!--- Build a table to display the contents ---> <cfoutput> <table cellpadding = "2 " cellspacing = "2 "> <TR> <TD><B>Employee</B></TD> <TD><B>Dept.</B></TD> </TR> <!--- In cfloop, use item to create a variable called person to hold value of key as loop runs ---> <cfloop collection = #Departments# item = "person "> <TR> <TD>#person#</TD> <TD>#StructFind(Departments, person)#</TD> </TR> </cfloop> </table> </cfoutput> ...