文章作者 : linkfoxo [ fengjf@novasprint.com ] Web URL : http://www.cfwindow.com
上载日期 : 2000-12-08
注意: 这是转自http://www.cfm-resources.com/members/comet 的资料,没有翻译,请见谅.
A structure, in ColdFusion, is considered to be a type of 'collection',
in that data held within the structure is related. You can dynamically
create collections using structures or you can use collections returned
from COM objects.
To loop through a structure, you use the ColdFusion "COLLECTION" and
"ITEM" attributes of CFLOOP:
<CFSET Employee = StructNew()>
<CFSET Employee.FirstName = "Dain">
<CFSET Employee.LastName = "Anderson">
<CFSET Employee.UserID = "1234">
<CFLOOP COLLECTION="#Employee#" ITEM="this">
<CFOUTPUT> #Evaluate("Employee." & this)#<BR> </CFOUTPUT>
</CFLOOP>
This would give you: Dain Anderson 1234 If you get an error with this
example, make sure you used ITEM= instead of INDEX= in your loop
statement. In another example, you could create an array and then
populate it with structures:
<!--- Let's do a simple query --->
<CFQUERY DATASOURCE="myDSN" NAME="getEmployees">
SELECT FirstName, LastName, UserID
FROM tblEmployees
ORDER BY LastName ASC
</CFQUERY>
<!--- Create an array to hold the data --->
<CFSET aEmployee = ArrayNew(1)>
<!--- Loop through the query --->
<CFLOOP QUERY="getEmployees">
<CFSET i = getEmployees.CurrentRow>
<!--- Create a structure as the array's first element --->
<CFSET aEmployee[i] = StructNew()>
<CFSET aEmployee[i].FirstName = "#FirstName#">
<CFSET aEmployee[i].LastName = "#LastName#">
<CFSET aEmployee[i].UserID = "#UserID#">
</CFLOOP>
<!--- To output the data, you can loop through the elements in the array
--->
<CFLOOP FROM="1" TO="#ArrayLen(aEmployee)#" INDEX="i">
<CFLOOP COLLECTION="#aEmployee[i]#" ITEM="this">
<CFOUTPUT> #Evaluate("aEmployee[i]." & this)#<BR> </CFOUTPUT>
</CFLOOP>
</CFLOOP>
|