Description

The cfloop collection attribute loops over every object within a COM/DCOM collection object, or every element in a structure:

  • A COM/DCOM collection object is a set of similar items referenced as a group. For example, the group of open documents in an application is a collection.
  • A structure contains a related set of items, or it can be used as an associative array. Looping is particularly useful when using a structure as an associative array.
    In the loop, each item is referenced by the variable name in the item attribute. The loop executes until all items have been accessed.
    The collection attribute is used with the item attribute. 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.
    For more information, see Integrating COM and CORBA Objects in CFML Applications in the Developing ColdFusion Applications.

Example

 

<cfscript>
departments = structNew("Ordered");
 /** On iterating this struct, you get the values in insertion order, which is the way you inserted the values. **/
 /** Create a structure and set its contents. **/
      
     departments.John = "Sales";
     departments.Tom = "Finance";
     departments.Mike = "Education";
     departments.Andrew = "Marketing";
      
 /** Build a table to display the contents **/
      
</cfscript>
<cfoutput >
<table cellpadding="2" cellspacing="2">
    <tr>
        <td><b>Employee</b></td>
        <td><b>Department</b></td>
    </tr>
<!--- Use cfloop to loop through the departments structure.The item attribute specifies a name for the structure key. --->
   <cfloop collection=#departments# item="person">
        <tr>
            <td>#person#</td>
            <td>#Departments[person]#</td>
        </tr>
    </cfloop>
</table>
</cfoutput>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy