loops in cf michael smith president teratech, inc coldfusion, database & vb custom development ...
Post on 11-Jan-2016
214 Views
Preview:
TRANSCRIPT
Loops in CFMichael Smith
President
TeraTech, Inc
ColdFusion, Database & VB custom development
http://www.teratech.com
800-447-9120
www.teratech.com
Introduction Loops - many types Many uses
The Challenge
SecuritySecurity
www.teratech.com
Loop TypesHere is what we will be covering:
For Loops While Loops Query Loops List Loops
More advanced loops not covered: Structure Loops COM Loops
www.teratech.com
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.
www.teratech.com
Loop uses
Repeating HTML Processing text Outputing queries Nested Loops Breaking out of loops Banding report lines
www.teratech.com
1.1 FOR Loops “FOR NEXT” loop
<CFLOOP INDEX="parameter_name"
FROM="beginning_value"TO="ending_value"
STEP="increment">Lines to repeat</CFLOOP>
Is INDEX LT TO? If so loop
again
INDEX = FROM
INDEX = INDEX + STEP
www.teratech.com
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
www.teratech.com
FOR Loop Example 1
<CFLOOP INDEX="LoopCount" FROM="5" TO="1" STEP="-1">
The loop index is #LoopCount#.<BR> </CFLOOP>(Assume inside CFOUTPUT tags)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.
www.teratech.com
FOR Loop Example 2
HTML list boxes of hours that goes from 0 to 23
<SELECT NAME="Hour"><CFLOOP INDEX="hour" FROM="0"
TO="23"> <CFOUTPUT> <OPTION
VALUE="#hour#">#hour# </CFOUTPUT></CFLOOP></SELECT>
www.teratech.com
Nested Loop Example
List box with all the hours and minutes of the day.
<SELECT NAME="HourAndMinutes"><CFLOOP INDEX="hour" FROM="0" TO="23"> <CFLOOP INDEX="minute" FROM="0"
TO="59"> <CFOUTPUT> <OPTION
VALUE="'#hour#:#minute#'">#hour#:#minute#
</CFOUTPUT> </CFLOOP></CFLOOP></SELECT>
That is 1440
items! Not a
good idea for a real
site
www.teratech.com
1.2 WHILE Loops “DO WHILE” loop
<CFSET StopIt = 0><CFLOOP CONDITION="StopIt LTE 5">
<CFSET StopIt = RandRange(1,10)>
<HR></CFLOOP>
Is condition true? If so loop again
www.teratech.com
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>
www.teratech.com
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!
www.teratech.com
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
www.teratech.com
Operator precedence
()IS, EQ, NEQ, LT, LE, GT, GE,
CONTAINSNOT ANDOR
www.teratech.com
CFBREAK
CFBREAK exits the current loop<CFSET StopIt = 0><CFLOOP CONDITION=“TRUE”> <CFSET StopIt = RandRange(1,10)>
<CFIF StopIt LTE 5><CFBREAK>
</CFIF> <CFOUTPUT></CFOUTPUT><HR></CFLOOP>More code here
www.teratech.com
1.3 Query Loops CFOUTPUT CFLOOP CFMAIL
www.teratech.com
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
Any records left? If so loop again
Start at first record
www.teratech.com
CFLOOP Query Loop
<CFQUERY NAME="GetEmail" DATASOURCE="Library">
SELECT Email FROM Customer </CFQUERY>
<CFLOOP QUERY="GetEmail"> <CFOUTPUT>#GetEmail.Email#<BR></
CFOUTPUT></CFLOOP>Any records
left? If so loop again
Start at first record
www.teratech.com
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="info@mycompany.com" SUBJECT=“Test” SERVER="smtp.mycompany.com">Hi There </CFMAIL>
Any records left? If so loop again
Start at first record
www.teratech.com
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="info@mycompany.com" SUBJECT="#GetText.EmailSubject#" SERVER="smtp.mycompany.com">#GetText.EmailText# </CFMAIL></CFLOOP>
www.teratech.com
Other recordsets
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
www.teratech.com
1.4 List Loops “FOR EACH” loop
<CFLOOP INDEX="ListElement" LIST="#form.state#" DELIMITERS=","> <CFOUTPUT>#ListElement#</CFOUTPUT><BR>
</CFLOOP> Other delimiters than comma are
allowed
Any items left in list? If so loop again
Start at first item in list
www.teratech.com
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>
www.teratech.com
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
www.teratech.com
List Loop example
List local states
<CFLOOP INDEX=“StateName”
LIST=“MD, VA, DC”>
<CFOUTPUT>#StateName#
</CFOUTPUT><BR>
</CFLOOP>
Produces…. MDVADC
www.teratech.com
Text file as a “list”
Text file processing by line can be done using lists
Read in text file using CFFILE Use CR LF as delimiter The list elements are now the lines
in the file.
www.teratech.com
1.5 Structure Loops Loop over all people in the
Department<CFLOOP
COLLECTION=#Departments# ITEM="person">
#person#,
#StructFind(Departments, person#)
</CFLOOP>
Any items left in
structure? If so loop again
Start at first item in
structure
www.teratech.com
1.6 COM Loops FOR EACH OBJECT Loops<CFLOOP COLLECTION=#FFUNC#
ITEM=file2>
<CFOUTPUT> #file2.name# <BR> </CFOUTPUT>
</CFLOOP>
www.teratech.com
CFSCRIPT Loops
CFScript is a JavaScript like language that provides the standard looping features of CFML plus a few more looping features
For While Do-while For-in CFScript also includes the continue and
break statements that control loop processing.
www.teratech.com
CFSCRIPT Loops syntax
FOR loopfor (inital-expression; test-
expression; final-expression) statement
WHILE loopwhile (expression) statement UNTIL loop – evaluates condition at
end of loopdo statement while (expression);
www.teratech.com
More CFSCRIPT loops
Structure loopfor (variable in structure)
statement The continue statement tells
ColdFusion to skip to the beginning of the next loop iteration.
The break statement exits the current loop or case statement. Similar to <CFBREAK>
Note: Still use LE etc for conditionals in CFSCRIPT and not the JavaScript <
www.teratech.com
CFSCRIPT for
for (inital-expression; test-expression; final-expression) statement
Evaluates the initial expression. Evaluates the test-expression. If the test-expression is False, exits the
loop and processing continues following the statement.If the test-expression is True: Executes the statement (or statement
block). Evaluates the final-expression. Loops
statement can be a single semicolon terminated statement or a statement block in curly braces.
www.teratech.com
for loop example
Assign array a(1) =1 etcfor(index=1; index LT 10; index
= index + 1) a[index]=index;
www.teratech.com
Infinite for loop example
Search for key using break to stop infinite loop
indx=0; for( ; ; ) { indx=indx+1;
if(Find("key",strings[indx],1)) { WriteOutput("Found key at " & indx & ".<br>"); break; }
else if (indx IS ArrayLen(strings)) { WriteOutput("Exited at " & indx & ".<br>"); break; }
}
www.teratech.com
Resources CFDOCS Ben Forta Books http://www.cfug-md.org/articles/
introCF-4-Lists.cfm http://www.houseoffusion.com
www.teratech.com
Questions
michael@teratech.com
top related