Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops in CF: To loop or not to loop? Neil Ross

Similar presentations


Presentation on theme: "Loops in CF: To loop or not to loop? Neil Ross"— Presentation transcript:

1 Loops in CF: To loop or not to loop? Neil Ross www.codesweeper.com neil@codesweeper.com

2 About Me Developing Web Sites and Apps since ’95 Worked for Allaire as CF Instructor and Consultant Bayer, 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 (2 nd Runner-Up) Ask about my Not-Yet-Famous Application Framework

3 Introduction Loops - many types Many uses

4 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

5 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.

6 Loop uses Repeating HTML Processing text Output queries Nested Loops Breaking out of loops Banding report lines

7 FOR Loops “FOR NEXT” loop <CFLOOP index="parameter_name“ from="beginning_value" to="ending_value"STEP="increment"> Lines to repeat

8 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

9 FOR Loop Example 1 The loop index is #LoopCount#. 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.

10 FOR Loop Example 2 HTML list boxes of hours that goes from 0 to 23 #hour#

11 Nested Loop Example List box with all the hours and minutes of the day. #hour#:#minute#

12 WHILE Loops “DO WHILE” loop Same syntax as CFIF conditional logic #dice#

13 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

14 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!

15 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

16 Operator precedence () IS, EQ, NEQ, LT, LE, GT, GE CONTAINS NOT AND OR

17 CFBREAK CFBREAK exits the current loop #StopIt# More code here

18 Query Loops Query loops can be generated by three ColdFusion constructs. Those include the following: CFOUTPUT CFLOOP CFMAIL

19 CFOUTPUT Query Loop SELECT Email FROM Customer #GetEmail.Email# Variable available: Queryname.currentrow Queryname.recordcount

20 CFLOOP Query Loop SELECT Email FROM Customer #GetEmail.Email#

21 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

22 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#

23 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

24 List Loops “FOR EACH” loop <CFLOOP INDEX="ListElement" LIST="#form.state#" DELIMITERS=","> #ListElement# Other delimiters than comma are allowed

25 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 Statements to loop through

26 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

27 List Loop example List local states <CFLOOP INDEX=“StateName” LIST=“MD, VA, DC”> #StateName# Produces…. MD VA DC

28 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.

29 Read text file code The following code reads a text file, then loops through the content of the file. #line# Count lines is #ListLen(text_file,"#CHR(13)#")#

30 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

31 CFSCRIPT Loops syntax FOR loop for (inital-expression; test-expression; final- expression) statement WHILE loop while (expression) statement UNTIL loop – evaluates condition in the loop do statement while (expression);

32 Resources Macromedia LiveDocs http://livedocs.macromedia.com/ http://livedocs.macromedia.com/

33 Questions Neil Ross www.codesweeper.com neil@codesweeper.com Thanks to Sandy Clark www.shayna.com


Download ppt "Loops in CF: To loop or not to loop? Neil Ross"

Similar presentations


Ads by Google