1 / 33

Loops in CF: To loop or not to loop?

Loops in CF: To loop or not to loop?. Neil Ross www.codesweeper.com neil@codesweeper.com. 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’

kiril
Download Presentation

Loops in CF: To loop or not to loop?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 (2nd Runner-Up) • Ask about my Not-Yet-Famous Application Framework

  3. Introduction • Loops - many types • Many uses

  4. Here is what we will be covering: For Loops While Loops Query Loops List Loops More advanced loops not covered: Structure Loops COM Loops Loop Types

  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 </CFLOOP>

  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. <CFOUTPUT> <CFLOOP index="LoopCount" from="5" TO="1" step="-1"> The loop index is #LoopCount#.<BR> </CFLOOP> </CFOUTPUT> This produces…. FOR Loop Example 1 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 <SELECT NAME="Hour"> <CFOUTPUT> <CFLOOP INDEX="hour" FROM="0" TO="23">    <OPTION VALUE="#hour#">#hour# </CFLOOP> </CFOUTPUT> </SELECT>

  11. Nested Loop Example • List box with all the hours and minutes of the day. <SELECT NAME="HourAndMinutes"> <CFOUTPUT> <CFLOOP INDEX="hour" FROM="0" TO="23">   <CFLOOP INDEX="minute" FROM="0" TO="59">         <OPTION VALUE="'#hour#:#minute#'">#hour#:#minute#   </CFLOOP> </CFLOOP> <CFOUTPUT> </SELECT>

  12. WHILE Loops • “DO WHILE” loop • Same syntax as CFIF conditional logic<CFSET Dice = 0><CFLOOP CONDITION="Dice LTE 5"> <CFSET Dice = RandRange(1,6)><CFOUTPUT>#dice#</CFOUTPUT><BR> </CFLOOP>

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

  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 <CFSET StopIt = 0> <CFLOOP CONDITION=“TRUE”> <CFSET StopIt = RandRange(1,10)> <CFIF StopIt LTE 5> <CFBREAK> </CFIF> <CFOUTPUT>#StopIt#</CFOUTPUT><BR> </CFLOOP> 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 <CFQUERY NAME="GetEmail"   DATASOURCE="Library">    SELECT Email FROM Customer </CFQUERY> <CFOUTPUT QUERY="GetEmail"> #GetEmail.Email#<BR> </CFOUTPUT> • Variable available: • Queryname.currentrow • Queryname.recordcount

  20. CFLOOP Query Loop <CFQUERY NAME="GetEmail"   DATASOURCE="Library">    SELECT Email FROM Customer </CFQUERY> <CFOUTPUT> <CFLOOP QUERY="GetEmail"> #GetEmail.Email#<BR> </CFLOOP> </CFOUTPUT>

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

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

  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 <CFOUTPUT><CFLOOP INDEX="ListElement" LIST="#form.state#" DELIMITERS=","> #ListElement#<BR> </CFLOOP> </CFOUTPUT> • 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 <CFLOOP INDEX=“list_variable” LIST=“value_list”> Statements to loop through </CFLOOP>

  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”> <CFOUTPUT>#StateName# </CFOUTPUT><BR> </CFLOOP> 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. <cffile action="READ" file="C:\afile.txt" variable="text_file"> <cfoutput> <CFLOOP list="#text_file#" index="line" delimiters="#CHR(13)#"> #line#<br> </CFLOOP> Count lines is #ListLen(text_file,"#CHR(13)#")# </CFOUTPUT>

  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 LiveDocshttp://livedocs.macromedia.com/

  33. Questions • Neil Ross www.codesweeper.comneil@codesweeper.com • Thanks to Sandy Clarkwww.shayna.com

More Related