home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 1999 April / APC443.iso / features / grpware / coldfus / coldfusi.exe / data1.cab / Documentation / snippets / cfloop.cfm < prev    next >
Encoding:
Text File  |  1998-10-08  |  2.4 KB  |  95 lines

  1. <CFQUERY NAME="GetEmployees" DATASOURCE="cfsnippets">
  2. SELECT     Emp_ID, FirstName, LastName, EMail, Phone, Department
  3. FROM       Employees
  4. </CFQUERY>
  5.  
  6. <!--- This example demonstrates the different kind
  7. of CFLOOP actions available --->
  8. <HTML>
  9.  
  10. <HEAD>
  11.  
  12. <TITLE>
  13. CFLOOP Example
  14. </TITLE>
  15. </HEAD>
  16.  
  17. <BODY>
  18.  
  19. <H3>CFLOOP Example</H3>
  20.  
  21. <P><B>Index loops</B>
  22. <BR>A simple loop: we can use an index parameter to
  23. tell us how many times we have looped through the expression.
  24. <P>
  25. <CFLOOP INDEX="counter" FROM="1" TO="5" STEP=1>
  26.     <CFOUTPUT>We have cycled through the loop #counter# time<CFIF counter is not 1>s</CFIF><BR></CFOUTPUT>
  27. </CFLOOP>
  28.  
  29. <P><B>Conditional loops</B>
  30. <BR>By stating a condition (tempVar is not 15), we can loop through
  31. and continue adding 3 onto tempVar; when tempVar is 15, the loop exits.
  32. <P>
  33. <CFPARAM name="tempVar" default=3>
  34. <CFLOOP CONDITION="tempVar is not 15">
  35.  
  36. <CFSET tempVar = tempVar + 3>
  37. <CFOUTPUT>#TempVar#</CFOUTPUT>
  38. </CFLOOP>
  39.  
  40. <P>Because tempVar now equals <CFOUTPUT>#tempVar#</CFOUTPUT>, 
  41. the condition is fulfilled.
  42.  
  43. <P><B>Looping over a query</B>
  44. <BR>Looping over a query can help us to display different
  45. output when a certain condition is reached (e.g. if the 
  46. Department of the employee is "Engineering," display in red):
  47.  
  48. <P>
  49. <CFLOOP QUERY="getEmployees">
  50.     <CFOUTPUT>
  51.         #FirstName# #LastName#,
  52.         <CFIF #Department# is "Engineering">
  53.             <font color=ff0000>#Department#</FONT>
  54.         <CFELSE>#Department#
  55.         </CFIF>
  56.         <BR>
  57.     </CFOUTPUT>
  58. </CFLOOP>
  59.  
  60. <P><B>Looping over a list</B>
  61. <P>CF gives the ability to loop over a list, which we
  62. can create for this example by looping over a new query
  63. and creating a list of FirstName/LastName combinations.
  64. <P>While looping through our query, we can create the list 
  65. variable, which we will then feed back to CFLOOP.
  66.  
  67. <CFQUERY NAME="GetFirstLastName" DATASOURCE="cfsnippets">
  68. SELECT FirstName, LastName
  69. FROM Employees
  70. ORDER by LastName
  71. </CFQUERY>
  72.  
  73. <!--- set up a temporary variable --->
  74. <CFSET tempList = "">
  75.  
  76. <!--- populate the list by looping through the query --->
  77. <CFLOOP QUERY="GetFirstLastName">
  78. <CFSET tempList = tempList & "#FirstName# #LastName#,">
  79. </CFLOOP>
  80.  
  81. <P>The list is as follows:
  82. <BR><CFOUTPUT>#tempList#</CFOUTPUT>
  83.  
  84. <P>CFLOOP outputs the list:
  85. <BR>
  86. <!--- loop through the resulting list --->
  87. <CFLOOP INDEX="ListElement" LIST="#tempList#" DELIMITERS=",">
  88.     <CFOUTPUT>#ListElement#</CFOUTPUT><BR>
  89. </CFLOOP>
  90.  
  91.  
  92. </BODY>
  93.  
  94. </HTML>       
  95.