Download presentation
Presentation is loading. Please wait.
Published byBlake Barrett Modified over 9 years ago
1
Loops in CF Michael Smith President TeraTech, Inc ColdFusion, Database & VB custom development http://www.teratech.com 800-447-9120
2
www.teratech.com Introduction n Loops - many types n Many uses
3
www.teratech.com 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
4
www.teratech.com What to use loops for n Use FOR loops when you know exactly how many times a set of statements should be executed n Use LIST loops when you want to loop over something other than numbers n Use WHILE loops when you want to execute a set of statements as long as a condition is True n Use QUERY loops when you want to repeat for all records in a query.
5
www.teratech.com Loop uses n Repeating HTML n Processing text n Outputing queries n Nested Loops n Breaking out of loops n Banding report lines
6
www.teratech.com 1.1 FOR Loops n “FOR NEXT” loop <CFLOOP INDEX="parameter_name" FROM="beginning_value" TO="ending_value" STEP="increment"> Lines to repeat Is INDEX LT TO? If so loop again INDEX = FROM INDEX = INDEX + STEP
7
www.teratech.com FOR CFLOOP Parameters n INDEX -name of variable that controls loop execution n FROM - starting loop value n TO - ending loop value n STEP – controls amount index variable is incremented (or decremented) in each loop iteration n Note: Loop may execute zero times if backwards - for example FROM 2 TO 1
8
www.teratech.com FOR Loop Example 1 The loop index is #LoopCount#. (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.
9
www.teratech.com FOR Loop Example 2 HTML list boxes of hours that goes from 0 to 23 #hour#
10
www.teratech.com Nested Loop Example List box with all the hours and minutes of the day. #hour#:#minute# That is 1440 items! Not a good idea for a real site
11
www.teratech.com 1.2 WHILE Loops n “DO WHILE” loop Is condition true? If so loop again
12
www.teratech.com WHILE Loop details n FOR and LIST loops are executed a certain number of times n WHILE loops are executed while a condition is true Statements to loop through
13
www.teratech.com WHILE Loop Parameters n WHILE Loops n CONDITION contains a logical expression that is evaluated before each loop iteration n As long as CONDITION is true – the loop is executed n Tip - Make sure you change the values of variables used in CONDITION expression in the loop body – otherwise infinite loop!
14
www.teratech.com Conditional operators n GT, LT, GTE, LTE, EQ, NEQ, IS, CONTAINS are the relational operators supported by ColdFusion n (don’t use > etc because CFML uses >) n AND, OR, NOT are the logical operators supported by ColdFusion n Use ()s to group expressions
15
www.teratech.com Operator precedence () IS, EQ, NEQ, LT, LE, GT, GE, CONTAINS NOT AND OR
16
www.teratech.com CFBREAK n CFBREAK exits the current loop More code here
17
www.teratech.com 1.3 Query Loops n CFOUTPUT n CFLOOP n CFMAIL
18
www.teratech.com CFOUTPUT Query Loop SELECT Email FROM Customer #GetEmail.Email# Variable available: Queryname.currentrow Queryname.recordcount Any records left? If so loop again Start at first record
19
www.teratech.com CFLOOP Query Loop SELECT Email FROM Customer #GetEmail.Email# Any records left? If so loop again Start at first record
20
www.teratech.com CFMAIL loop Send one email for each record in the query SELECT Email FROM Customer <CFMAIL QUERY="GetEmail" TO="#GetEmail.Email#" FROM="info@mycompany.com" SUBJECT=“Test” SERVER="smtp.mycompany.com"> Hi There Any records left? If so loop again Start at first record
21
www.teratech.com Nested Query Loop Example SELECT Email, SecurityLevel FROM Customer SELECT EmailText, EmailSubject FROM Messages WHERE SecurityLevel = #GetEmail.SecurityLevel# <CFMAIL QUERY="GetText" TO="#GetEmail.Email#" FROM="info@mycompany.com" SUBJECT="#GetText.EmailSubject#" SERVER="smtp.mycompany.com">#GetText.EmailText#
22
www.teratech.com Other recordsets n You can loop over record sets from other tags than CFQUERY: n CFDIRECTORY – file list n CFPOP – read email n CFSEARCH – Verity text search n CFLDAP – LDAP records n CFWDDX
23
www.teratech.com 1.4 List Loops n “FOR EACH” loop <CFLOOP INDEX="ListElement" LIST="#form.state#" DELIMITERS=","> #ListElement# u Other delimiters than comma are allowed Any items left in list? If so loop again Start at first item in list
24
www.teratech.com How list loops work n 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
25
www.teratech.com List Loop parameters n INDEX - variable that controls loop execution n LIST - a list of comma separated values n INDEX is assigned the values in list one at a time as the loop executes n DELIMITERS – optional to give a delimiter other than comma
26
www.teratech.com List Loop example n List local states <CFLOOP INDEX=“StateName” LIST=“MD, VA, DC”> #StateName# Produces…. MD VA DC
27
www.teratech.com Text file as a “list” n Text file processing by line can be done using lists n Read in text file using CFFILE n Use CR LF as delimiter n The list elements are now the lines in the file.
28
www.teratech.com 1.5 Structure Loops n Loop over all people in the Department #person#, #StructFind(Departments, person#) Any items left in structure? If so loop again Start at first item in structure
29
www.teratech.com 1.6 COM Loops n FOR EACH OBJECT Loops #file2.name#
30
www.teratech.com CFSCRIPT Loops n CFScript is a JavaScript like language that provides the standard looping features of CFML plus a few more looping features n For n While n Do-while n For-in CFScript also includes the continue and break statements that control loop processing.
31
www.teratech.com CFSCRIPT Loops syntax n FOR loop for (inital-expression; test- expression; final-expression) statement n WHILE loop while (expression) statement n UNTIL loop – evaluates condition at end of loop do statement while (expression);
32
www.teratech.com More CFSCRIPT loops n Structure loop for (variable in structure) statement n The continue statement tells ColdFusion to skip to the beginning of the next loop iteration. n The break statement exits the current loop or case statement. Similar to n Note: Still use LE etc for conditionals in CFSCRIPT and not the JavaScript <
33
www.teratech.com CFSCRIPT for n for (inital-expression; test-expression; final-expression) statement n Evaluates the initial expression. n Evaluates the test-expression. n If the test-expression is False, exits the loop and processing continues following the statement.If the test-expression is True: u Executes the statement (or statement block). u Evaluates the final-expression. u Loops statement can be a single semicolon terminated statement or a statement block in curly braces.
34
www.teratech.com for loop example n Assign array a(1) =1 etc for(index=1; index LT 10; index = index + 1) a[index]=index;
35
www.teratech.com Infinite for loop example n 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 & ". "); break; } else if (indx IS ArrayLen(strings)) { WriteOutput("Exited at " & indx & ". "); break; } }
36
www.teratech.com Resources n CFDOCS n Ben Forta Books n http://www.cfug- md.org/articles/introCF-4-Lists.cfm n http://www.houseoffusion.com
37
www.teratech.com Questions n michael@teratech.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.