loops in cf sandra clark senior software developer constella group [email protected]

Download Loops in CF Sandra Clark Senior Software Developer Constella Group sclark@constellagroup.com

If you can't read please download the document

Upload: ralf-king

Post on 13-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1

Loops in CF Sandra Clark Senior Software Developer Constella Group [email protected] Slide 2 2 Introduction Loops - many types Many uses Slide 3 3 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 Slide 4 4 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. Slide 5 5 Loop uses Repeating HTML Processing text Outputing queries Nested Loops Breaking out of loops Banding report lines Slide 6 Lines to repeat Is INDEX LT ">Lines to repeat Is INDEX LT TO? If so loop INDEX = FROM INDEX = INDEX + STEP">Lines to repeat Is INDEX LT " title="6 1.1 FOR Loops FOR NEXT loopLines to repeat Is INDEX LT "> 6 1.1 FOR Loops FOR NEXT loopLines to repeat Is INDEX LT TO? If so loop INDEX = FROM INDEX = INDEX + STEP Slide 7 7 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 Slide 8 8 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. Must have CFOUTPUT Slide 9 9 FOR Loop Example 2 HTML list boxes of hours that goes from 0 to 23 #hour# Slide 10 10 Nested Loop Example List box with all the hours and minutes of the day. #hour#:#minute# This is 1440 items! Not a good idea for a real site Slide 11 11 1.2 WHILE Loops DO WHILE loop #dice# Is condition true? If so loop again Final loop run has condition false! Same syntax as CFIF conditions Slide 12 12 WHILE Loop details FOR and LIST loops are executed a certain number of times WHILE loops are executed while a condition is true Statements to loop through Slide 13 13 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! Slide 14 14 Conditional operators GT, LT, GTE, LTE, EQ, NEQ, IS, CONTAINS are the relational operators supported by ColdFusion (dont use > etc because CFML uses >) AND, OR, NOT are the logical operators supported by ColdFusion Use ()s to group expressions Slide 15 15 Operator precedence () IS, EQ, NEQ, LT, LE, GT, GE, CONTAINS NOT AND OR Slide 16 16 CFBREAK CFBREAK exits the current loop #StopIt# More code here Slide 17 17 1.3 Query Loops CFOUTPUT CFLOOP CFMAIL Slide 18 18 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 Slide 19 19 CFLOOP Query Loop SELECT Email FROM Customer #GetEmail.Email# Any records left? If so loop again Start at first record CFOUTPUT required Slide 20Hi There Any records left? If so loop again Start at first record"> 20 CFMAIL loop Send one email for each record in the query SELECT Email FROM CustomerHi There Any records left? If so loop again Start at first record Slide 21 #GetText.EmailText#"> 21 Nested Query Loop Example SELECT Email, SecurityLevel FROM Customer SELECT EmailText, EmailSubject FROM Messages WHERE SecurityLevel = #GetEmail.SecurityLevel# #GetText.EmailText# Slide 22 22 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 Slide 23 #ListElement# Other delimiters than comma are allow">#ListElement# Other delimiters than comma are allowed Any items left in list? If so loop again Start at first item in list">#ListElement# Other delimiters than comma are allow" title="23 1.4 List Loops FOR EACH loop#ListElement# Other delimiters than comma are allow"> 23 1.4 List Loops FOR EACH loop#ListElement# Other delimiters than comma are allowed Any items left in list? If so loop again Start at first item in list Slide 24 24 How list loops work LIST loops allow you to list the values for the control variable instead of computing them as in the FOR loopStatements to loop through Slide 25 25 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 Slide 26 26 List Loop example List local states#StateName# Produces. MD VA DC Slide 27 27 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. Slide 28 28 Read text file code #line# Count lines is #ListLen(text_file,"#CHR(13)#")# Slide 29 29 1.5 Structure Loops 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 Slide 30 30 1.6 COM Loops FOR EACH OBJECT Loops #file2.name# Slide 31 31 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. Slide 32 32 CFSCRIPT 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); Slide 33 33 More 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 Note: Still use LTE etc for conditionals in CFSCRIPT and not the JavaScript