;中国 COLD FUSION 用户组; WWW.CFWINDOW.COM 
您的位置 :首页 >> CF 技巧文章 >> 阅读文章内容 [ 关闭窗口 ]      

技巧文章内容 
    怎样在Structure中实现循环. [ 英文 ]
文章作者 : 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>







< 联系我们 --- 中国Cold Fusion用户组>

CFUG 国内(总部):Linkfoxo    上海:CFANS    北京:Cafe,Cyberkid,liwater    沈阳:Wangking
  
哈尔滨:Baiming    浙江:梅盛松    江西:陈末
  
CFUG (国际) Nagoya(名古屋):Codeguru    新加坡:YUZI    新西兰(Auckland):Richard CHEN
Copyright 2000-2001 www.cfwindow.com.All rights reserved

;中国 COLD FUSION 用户组; WWW.CFWINDOW.COM