About Me• Developing Web Sites and Apps since ’95
• Worked for Allaire as CF Instructor and ConsultantBayer, Lockheed, US Gov, State Govs
• Articles in CFDJ, ‘Inside ColdFusion MX’
• Speaker at CF Dev Conf 2000, CFEurope 2003, CFUN03
• Freelance application design and development as Codesweeper
CFDJ Award Winner for PhotoFolio app (2nd Runner-Up)Ask about my Not-Yet-Famous Application Framework
Loop Types
Here is what we will be covering: For Loops While Loops Query Loops List Loops
More advanced loops not covered: Structure Loops COM Loops
What to use loops for• Use FOR loops when you know exactly how many
times a set of statements should be executed
• Use LIST loops when you want to loop over something other than numbers
• Use WHILE loops when you want to execute a set of statements as long as a condition is True
• Use QUERY loops when you want to repeat for all records in a query.
Loop uses• Repeating HTML
• Processing text
• Output queries
• Nested Loops
• Breaking out of loops
• Banding report lines
FOR Loops
• “FOR NEXT” loop
<CFLOOP
index="parameter_name“ from="beginning_value"
to="ending_value"STEP="increment">
Lines to repeat
</CFLOOP>
FOR CFLOOP Parameters• INDEX -name of variable that controls loop
execution
• FROM - starting loop value
• TO - ending loop value
• STEP – controls amount index variable is incremented (or decremented) in each loop iteration
• Note: Loop may execute zero times if backwards - for example FROM 2 TO 1
FOR Loop Example 1<CFOUTPUT>
<CFLOOP index="LoopCount" from="5" TO="1" step="-1">
The loop index is #LoopCount#.<BR>
</CFLOOP>
</CFOUTPUT>
This produces….
The loop index is 5. The loop index is 4. The loop index is 3. The loop index is 2. The loop index is 1.
FOR Loop Example 2
• HTML list boxes of hours that goes from 0 to 23
<SELECT NAME="Hour">
<CFOUTPUT>
<CFLOOP INDEX="hour" FROM="0" TO="23">
<OPTION VALUE="#hour#">#hour#
</CFLOOP>
</CFOUTPUT>
</SELECT>
Nested Loop Example• List box with all the hours and
minutes of the day.
<SELECT NAME="HourAndMinutes"><CFOUTPUT>
<CFLOOP INDEX="hour" FROM="0" TO="23"> <CFLOOP INDEX="minute" FROM="0" TO="59">
<OPTION VALUE="'#hour#:#minute#'">#hour#:#minute# </CFLOOP></CFLOOP>
<CFOUTPUT></SELECT>
WHILE Loops
• “DO WHILE” loop Same syntax as CFIF conditional logic
<CFSET Dice = 0><CFLOOP CONDITION="Dice LTE 5"> <CFSET Dice = RandRange(1,6)><CFOUTPUT>#dice#</CFOUTPUT><BR>
</CFLOOP>
WHILE Loop Details• FOR and LIST loops are executed a certain number
of times
• WHILE loops are executed while a condition is true
<CFLOOP CONDITION=“while-condition”>Statements to loop through</CFLOOP>
WHILE Loop Parameters
• WHILE Loops
• CONDITION contains a logical expression that is evaluated before each loop iteration
• As long as CONDITION is true – the loop is executed
• Tip - Make sure you change the values of variables used in CONDITION expression in the loop body – otherwise infinite loop!
Conditional operators
• GT, LT, GTE, LTE, EQ, NEQ, IS, CONTAINS are the relational operators supported by ColdFusion
• (don’t use > etc because CFML uses >)
• AND, OR, NOT are the logical operators supported by ColdFusion
• Use ()s to group expressions
CFBREAK
• CFBREAK exits the current loop
<CFSET StopIt = 0>
<CFLOOP CONDITION=“TRUE”>
<CFSET StopIt = RandRange(1,10)>
<CFIF StopIt LTE 5>
<CFBREAK>
</CFIF>
<CFOUTPUT>#StopIt#</CFOUTPUT><BR>
</CFLOOP>
More code here
Query Loops
Query loops can be generated by three ColdFusion constructs. Those include the following:
• CFOUTPUT
• CFLOOP
• CFMAIL
CFOUTPUT Query Loop<CFQUERY NAME="GetEmail" DATASOURCE="Library"> SELECT Email FROM Customer </CFQUERY>
<CFOUTPUT QUERY="GetEmail"> #GetEmail.Email#<BR></CFOUTPUT>
• Variable available: Queryname.currentrow Queryname.recordcount
CFLOOP Query Loop
<CFQUERY NAME="GetEmail" DATASOURCE="Library">
SELECT Email FROM Customer
</CFQUERY>
<CFOUTPUT>
<CFLOOP QUERY="GetEmail">
#GetEmail.Email#<BR>
</CFLOOP>
</CFOUTPUT>
CFMAIL loop• Send one email for each record in the query
<CFQUERY NAME="GetEmail" DATASOURCE="Library"> SELECT Email FROM Customer </CFQUERY> <CFMAIL QUERY="GetEmail" TO="#GetEmail.Email#" FROM="[email protected]" SUBJECT=“Test” SERVER="smtp.mycompany.com">Hi There </CFMAIL>
Nested Query Loop Example
<CFQUERY NAME="GetEmail" DATASOURCE="Library"> SELECT Email , SecurityLevel FROM Customer </CFQUERY> <CFLOOP QUERY="GetEmail"> <CFQUERY NAME="GetText" DATASOURCE="Library"> SELECT EmailText, EmailSubject FROM Messages WHERE SecurityLevel = #GetEmail.SecurityLevel# </CFQUERY> <CFMAIL QUERY="GetText" TO="#GetEmail.Email#" FROM="[email protected]" SUBJECT="#GetText.EmailSubject#" SERVER="smtp.mycompany.com">#GetText.EmailText# </CFMAIL></CFLOOP>
Other record sets• You can loop over record sets from other tags than
CFQUERY:
CFDIRECTORY – file list CFPOP – read email CFSEARCH – Verity text search CFLDAP – LDAP records CFWDDX
List Loops
• “FOR EACH” loop
<CFOUTPUT><CFLOOP INDEX="ListElement" LIST="#form.state#" DELIMITERS=",">
#ListElement#<BR> </CFLOOP>
</CFOUTPUT>
Other delimiters than comma are allowed
How list loops work
• LIST loops allow you to list the values for the control variable instead of computing them as in the FOR loop
<CFLOOP INDEX=“list_variable” LIST=“value_list”>
Statements to loop through
</CFLOOP>
List Loop parameters• INDEX - variable that controls loop execution
• LIST - a list of comma separated values
• INDEX is assigned the values in list one at a time as the loop executes
• DELIMITERS – optional to give a delimiter other than comma
List Loop example
• List local states
<CFLOOP INDEX=“StateName”
LIST=“MD, VA, DC”>
<CFOUTPUT>#StateName#
</CFOUTPUT><BR>
</CFLOOP>
Produces….MDVADC
Text file as a “list”• Text file processing by line can be done using lists
• Read in text file using CFFILE
• Use CR as delimiter
• The list elements are now the lines in the file.
Read text file code
• The following code reads a text file, then loops through the content of the file.
<cffile action="READ" file="C:\afile.txt" variable="text_file">
<cfoutput><CFLOOP list="#text_file#" index="line" delimiters="#CHR(13)#">
#line#<br></CFLOOP>
Count lines is #ListLen(text_file,"#CHR(13)#")#
</CFOUTPUT>
CFSCRIPT Loops• CFScript is a JavaScript like language that
provides the standard looping features of CFML plus a few more
• For
• While
• Do-while
• For-in
CFSCRIPT Loops syntax• FOR loop
for (inital-expression; test-expression; final-expression) statement
• WHILE loopwhile (expression) statement
• UNTIL loop – evaluates condition in the loopdo statement while (expression);
Top Related