Loops in CF Michael Smith President Tera Tech
- Slides: 37
Loops in CF Michael Smith President Tera. Tech, Inc Cold. Fusion, Database & VB custom development http: //www. teratech. com 800 -447 -9120
Introduction n n Loops - many types Many uses
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
What to use loops for n n www. teratech. com 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.
Loop uses n n n www. teratech. com Repeating HTML Processing text Outputing queries Nested Loops Breaking out of loops Banding report lines
INDEX = FROM 1. 1 FOR Loops n “FOR NEXT” loop <CFLOOP INDEX="parameter_name" FROM="beginning_value" TO="ending_value" INDEX = INDEX + STEP Is INDEX LT TO? If so loop again STEP="increment"> Lines to repeat </CFLOOP>
FOR CFLOOP Parameters n n n www. teratech. com 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
FOR Loop Example 1 <CFLOOP INDEX="Loop. Count" FROM="5" TO="1" STEP="-1"> The loop index is #Loop. Count#. <BR> </CFLOOP> (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. www. teratech. com
FOR Loop Example 2 HTML list boxes of hours that goes from 0 to 23 <SELECT NAME="Hour"> <CFLOOP INDEX="hour" FROM="0" TO="23"> <CFOUTPUT> <OPTION VALUE="#hour#">#hour# </CFOUTPUT> </CFLOOP> </SELECT> n www. teratech. com
That is 1440 items! Not a good idea for a real site Nested Loop Example n List box with all the hours and minutes of the day. <SELECT NAME="Hour. And. Minutes"> <CFLOOP INDEX="hour" FROM="0" TO="23"> <CFLOOP INDEX="minute" FROM="0" TO="59"> <CFOUTPUT> <OPTION VALUE="'#hour#: #minute#'">#hour#: #minute# </CFOUTPUT> </CFLOOP> </SELECT> www. teratech. com
Is condition true? If so loop again 1. 2 WHILE Loops n “DO WHILE” loop <CFSET Stop. It = 0> <CFLOOP CONDITION="Stop. It LTE 5"> <CFSET Stop. It = Rand. Range(1, 10)> <HR> </CFLOOP>
WHILE Loop details FOR and LIST loops are executed a certain number of times n WHILE loops are executed while a condition is true <CFLOOP CONDITION=“while-condition”> Statements to loop through </CFLOOP> n www. teratech. com
WHILE Loop Parameters n n www. teratech. com 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!
Conditional operators n n www. teratech. com GT, LT, GTE, LTE, EQ, NEQ, IS, CONTAINS are the relational operators supported by Cold. Fusion (don’t use > etc because CFML uses >) AND, OR, NOT are the logical operators supported by Cold. Fusion Use ()s to group expressions
Operator precedence () IS, EQ, NEQ, LT, LE, GT, GE, CONTAINS NOT AND OR www. teratech. com
CFBREAK n CFBREAK exits the current loop <CFSET Stop. It = 0> <CFLOOP CONDITION=“TRUE”> <CFSET Stop. It = Rand. Range(1, 10)> <CFIF Stop. It LTE 5> <CFBREAK> </CFIF> <CFOUTPUT></CFOUTPUT><HR> </CFLOOP> More code here www. teratech. com
1. 3 Query Loops n n n CFOUTPUT CFLOOP CFMAIL
Start at first record Any records left? If so loop again www. teratech. com CFOUTPUT Query Loop <CFQUERY NAME="Get. Email" DATASOURCE="Library"> SELECT Email FROM Customer </CFQUERY> <CFOUTPUT QUERY="Get. Email"> #Get. Email#<BR> </CFOUTPUT> n Variable available: u Queryname. currentrow u Queryname. recordcount
CFLOOP Query Loop Start at first record Any records left? If so loop again www. teratech. com <CFQUERY NAME="Get. Email" DATASOURCE="Library"> SELECT Email FROM Customer </CFQUERY> <CFLOOP QUERY="Get. Email"> <CFOUTPUT>#Get. Email#<BR></CFOUTPU T> </CFLOOP>
CFMAIL loop Start at first record Any records left? If so loop again www. teratech. com n Send one email for each record in the query <CFQUERY NAME="Get. Email" DATASOURCE="Library"> SELECT Email FROM Customer </CFQUERY> <CFMAIL QUERY="Get. Email" TO="#Get. Email#" FROM="info@mycompany. com" SUBJECT=“Test” SERVER="smtp. mycompany. com"> Hi There </CFMAIL>
Nested Query Loop Example <CFQUERY NAME="Get. Email" DATASOURCE="Library"> SELECT Email , Security. Level FROM Customer </CFQUERY> <CFLOOP QUERY="Get. Email"> <CFQUERY NAME="Get. Text" DATASOURCE="Library"> SELECT Email. Text, Email. Subject FROM Messages WHERE Security. Level = #Get. Email. Security. Level# </CFQUERY> <CFMAIL QUERY="Get. Text" TO="#Get. Email#" FROM="info@mycompany. com" SUBJECT="#Get. Text. Email. Subject#" SERVER="smtp. mycompany. com">#Get. Text. Email. Text# </CFMAIL> </CFLOOP> www. teratech. com
Other recordsets n n n www. teratech. com 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
Start at first item in list Any items left in list? If so loop again 1. 4 List Loops n “FOR EACH” loop <CFLOOP INDEX="List. Element" LIST="#form. state#" DELIMITERS=", "> <CFOUTPUT>#List. Element# </CFOUTPUT><BR> </CFLOOP> u Other delimiters than comma are allowed
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> n www. teratech. com
List Loop parameters n n www. teratech. com 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
List Loop example List local states <CFLOOP INDEX=“State. Name” LIST=“MD, VA, DC”> <CFOUTPUT>#State. Name# </CFOUTPUT><BR> </CFLOOP> Produces…. MD n VA DC www. teratech. com
Text file as a “list” n n www. teratech. com Text file processing by line can be done using lists Read in text file using CFFILE Use CR LF as delimiter The list elements are now the lines in the file.
Start at first item in structure Any items left in structure? If so loop again 1. 5 Structure Loops Loop over all people in the Department <CFLOOP COLLECTION=#Departments# ITEM="person"> n #person#, #Struct. Find(Departments, person#) </CFLOOP>
1. 6 COM Loops FOR EACH OBJECT Loops <CFLOOP COLLECTION=#FFUNC# ITEM=file 2> n <CFOUTPUT> #file 2. name# <BR> </CFOUTPUT> </CFLOOP>
CFSCRIPT Loops n n n www. teratech. com CFScript is a Java. Script 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.
CFSCRIPT Loops syntax FOR loop for (inital-expression; testexpression; final-expression) statement n WHILE loop while (expression) statement n UNTIL loop – evaluates condition at end of loop do statement while (expression); n www. teratech. com
More CFSCRIPT loops Structure loop for (variable in structure) statement n The continue statement tells Cold. Fusion to skip to the beginning of the next loop iteration. n The break statement exits the current loop or case statement. Similar to <CFBREAK> n Note: Still use LE etc for conditionals in CFSCRIPT and not the Java. Script < n www. teratech. com
CFSCRIPT for n n statement can be a single semicolon terminated statement or a statement block in curly braces. n n 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). u Evaluates the final-expression. u Loops u www. teratech. com
for loop example Assign array a(1) =1 etc for(index=1; index LT 10; index = index + 1) a[index]=index; n www. teratech. com
Infinite for loop example Search for key using break to stop infinite loop indx=0; for( ; ; ) { indx=indx+1; if(Find("key", strings[indx], 1)) { Write. Output("Found key at " & indx & ". "); break; } else if (indx IS Array. Len(strings)) { Write. Output("Exited at " & indx & ". "); break; } } n www. teratech. com
Resources n n CFDOCS Ben Forta Books http: //www. cfugmd. org/articles/intro. CF-4 -Lists. cfm http: //www. houseoffusion. com
Questions n www. teratech. com michael@teratech. com
- President vice president treasurer secretary
- High tech low tech
- Give thanks michael w smith
- Michael smith foreign study supplement
- Michael prince texas tech
- Intestinal obstruction classification
- Loops in sequence diagram
- Nested loops storytelling
- Structured text array initialization
- Cakewalk loop construction
- What is nested loop
- Dynamic vs adynamic obstruction
- Reddish loops of gas
- For while loop matlab
- Signal flow
- Broadcasts packets, but creates loops in the systems.
- Nested loops python
- Complexity of 3 nested for loops
- Duodenal atresia jaundice
- Nodes branches and loops formula
- Lesson 7 sticker art with loops
- Loops in pascal
- N nested for loops
- Forward path in signal flow graph
- Nested for loops matlab
- Be 8255
- Sentinel loop in python
- Kvl loop
- Its platinum loops shrink to a wedding-ring
- Geothermal horizontal loop design
- Seninelle
- While loops and if-else structures
- Small basic turtle commands
- Adobe audition loops
- For loop condition
- Mesh analysis 3 loops
- Loops in tosca
- Types of loops in matlab