Title: Loops in CF
1Loops in CF
- Michael Smith
- www.teratech.com
- michael_at_teratech.com
- Sandra Clark
- http//www.shayna.com
- slclark_at_shayna.com
2Introduction
- Loops - many types
- Many uses
3Loop 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
4What 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.
5Loop uses
- Repeating HTML
- Processing text
- Output queries
- Nested Loops
- Breaking out of loops
- Banding report lines
61.1 FOR Loops
INDEX FROM
- FOR NEXT loop
- ltCFLOOP INDEX"parameter_name"
- FROM"beginning_value"
- TO"ending_value"
- STEP"increment"gt
- Lines to repeat
- lt/CFLOOPgt
Is INDEX LT TO? If so loop
INDEX INDEX STEP
7FOR 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
8FOR Loop Example 1
Must have CFOUTPUT
- ltCFLOOP INDEX"LoopCount" FROM"5" TO"1"
STEP"-1"gt ltCFOUTPUTgt - The loop index is LoopCount.ltBRgt lt/CFOUTPUTgt
- lt/CFLOOPgt
- (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.
9FOR Loop Example 2
- HTML list boxes of hours that goes from 0 to 23
- ltSELECT NAME"Hour"gt
- ltCFLOOP INDEX"hour" FROM"0" TO"23"gt
- ltCFOUTPUTgt
- ltOPTION VALUE"hour"gthour
- lt/CFOUTPUTgt
- lt/CFLOOPgt
- lt/SELECTgt
10Nested Loop Example
This is 1440 items! Not a good idea for a real
site
- List box with all the hours and minutes of the
day. - ltSELECT NAME"HourAndMinutes"gt
- ltCFLOOP INDEX"hour" FROM"0" TO"23"gt
- ltCFLOOP INDEX"minute" FROM"0" TO"59"gt
- ltCFOUTPUTgt
- ltOPTION VALUE"'hourminute'"gthourminu
te - lt/CFOUTPUTgt
- lt/CFLOOPgt
- lt/CFLOOPgt
- lt/SELECTgt
111.2 WHILE Loops
Same syntax as CFIF conditions
- DO WHILE loop
- ltCFSET Dice 0gt
- ltCFLOOP CONDITION"Dice LTE 5"gt
- ltCFSET Dice RandRange(1,6)gt
- ltCFOUTPUTgtdicelt/CFOUTPUTgt
- ltBRgt
- lt/CFLOOPgt
Is condition true? If so loop again
Final loop run has condition false!
12WHILE Loop details
- FOR and LIST loops are executed a certain number
of times - WHILE loops are executed while a condition is
true - ltCFLOOP CONDITIONwhile-conditiongt
- Statements to loop through
- lt/CFLOOPgt
13WHILE 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!
14Conditional operators
- GT, LT, GTE, LTE, EQ, NEQ, IS, CONTAINS are the
relational operators supported by ColdFusion - (dont use gt etc because CFML uses gt)
- AND, OR, NOT are the logical operators supported
by ColdFusion - Use ()s to group expressions
15Operator precedence
- ()
- IS, EQ, NEQ, LT, LE, GT, GE, CONTAINS
- NOT
- AND
- OR
16CFBREAK
- CFBREAK exits the current loop
- ltCFSET StopIt 0gt
- ltCFLOOP CONDITIONTRUEgt
- ltCFSET StopIt RandRange(1,10)gt
- ltCFIF StopIt LTE 5gt
- ltCFBREAKgt
- lt/CFIFgt
- ltCFOUTPUTgtStopItlt/CFOUTPUTgtltBRgt
- lt/CFLOOPgt
- More code here
171.3 Query Loops
18CFOUTPUT Query Loop
- ltCFQUERY NAME"GetEmail" DATASOURCE"Library"gt
- SELECT Email FROM Customer
- lt/CFQUERYgt
- ltCFOUTPUT QUERY"GetEmail"gt
- GetEmail.EmailltBRgt
- lt/CFOUTPUTgt
- Variable available
- Queryname.currentrow
- Queryname.recordcount
Start at first record
Any records left? If so loop again
19CFLOOP Query Loop
- ltCFQUERY NAME"GetEmail" DATASOURCE"Library"gt
- SELECT Email FROM Customer
- lt/CFQUERYgt
- ltCFLOOP QUERY"GetEmail"gt
- ltCFOUTPUTgtGetEmail.EmailltBRgtlt/CFOUTPUTgt
- lt/CFLOOPgt
CFOUTPUT required
Start at first record
Any records left? If so loop again
20CFMAIL loop
- Send one email for each record in the query
- ltCFQUERY NAME"GetEmail" DATASOURCE"Library"gt
- SELECT Email FROM Customer
- lt/CFQUERYgt
- ltCFMAIL QUERY"GetEmail"
- TO"GetEmail.Email"
- FROM"info_at_mycompany.com"
- SUBJECTTest
- SERVER"smtp.mycompany.com"gt
- Hi There
- lt/CFMAILgt
Start at first record
Any records left? If so loop again
21Nested Query Loop Example
- ltCFQUERY NAME"GetEmail" DATASOURCE"Library"gt
- SELECT Email , SecurityLevel FROM Customer
- lt/CFQUERYgt
- ltCFLOOP QUERY"GetEmail"gt
- ltCFQUERY NAME"GetText" DATASOURCE"Library"gt
- SELECT EmailText, EmailSubject FROM
Messages - WHERE SecurityLevel GetEmail.SecurityLev
el - lt/CFQUERYgt
- ltCFMAIL QUERY"GetText"
- TO"GetEmail.Email"
- FROM"info_at_mycompany.com"
- SUBJECT"GetText.EmailSubject"
- SERVER"smtp.mycompany.com"gtGetText.EmailTe
xt - lt/CFMAILgt
- lt/CFLOOPgt
22Other 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
231.4 List Loops
- FOR EACH loop
- ltCFLOOP INDEX"ListElement"
- LIST"form.state"
- DELIMITERS","gt
- ltCFOUTPUTgtListElement
- lt/CFOUTPUTgtltBRgt
- lt/CFLOOPgt
- Other delimiters than comma are allowed
Start at first item in list
Any items left in list? If so loop again
24How list loops work
- LIST loops allow you to list the values for the
control variable instead of computing them as in
the FOR loop - ltCFLOOP INDEXlist_variable
- LISTvalue_listgt
- Statements to loop through
- lt/CFLOOPgt
25List 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
26List Loop example
- List local states
- ltCFLOOP INDEXStateName
- LISTMD, VA, DCgt
- ltCFOUTPUTgtStateName
- lt/CFOUTPUTgtltBRgt
- lt/CFLOOPgt
- Produces.
MD VA DC
27Text 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.
28Read text file code
- ltcffile action"READ" file"C\afile.txt"
variable"text_file"gt - ltCFLOOP list"text_file" index"line"
delimiters"CHR(13)"gt - ltcfoutputgtlineltbrgtlt/cfoutputgt
- lt/CFLOOPgt
- ltCFOUTPUTgtCount lines is
- ListLen(text_file,"CHR(13)") lt/CFOUTPUTgt
291.5 Structure Loops
- Loop over all people in the Department
- ltCFLOOP COLLECTIONDepartments
- ITEM"person"gt
- person,
- StructFind(Departments, person)
- lt/CFLOOPgt
Start at first item in structure
Any items left in structure? If so loop again
301.6 COM Loops
- FOR EACH OBJECT Loops
- ltCFLOOP COLLECTIONFFUNC ITEMfile2gt
- ltCFOUTPUTgt file2.name ltBRgt lt/CFOUTPUTgt
- lt/CFLOOPgt
31CFSCRIPT 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.
32CFSCRIPT Loops syntax
- FOR loop
- for (inital-expression test-expression
final-expression) statement - WHILE loop
- while (expression) statement
- UNTIL loop evaluates condition at end of loop
- do statement while (expression)
33More CFSCRIPT loops
- Structure loop
- for (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 ltCFBREAKgt - Note Still use LTE etc for conditionals in
CFSCRIPT and not the JavaScript lt
34CFSCRIPT 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
35for loop example
- Assign array a(1) 1 etc
- for(index1 index LT 10 index index 1)
aindexindex
36Infinite for loop example
- Search for key using break to stop infinite loop
- indx0
- for( )
- indxindx1 if(Find("key",stringsindx,1))
- WriteOutput("Found key at " indx ".ltbrgt")
break - else if (indx IS ArrayLen(strings))
- WriteOutput("Exited at " indx ".ltbrgt")
break -
37Resources
- CFDOCS
- Ben Forta Books
- http//www.cfug-md.org/articles/introCF-4-Lists.cf
m - http//www.houseoffusion.com
38Questions