Loops in CF: To loop or not to loop? - PowerPoint PPT Presentation

About This Presentation
Title:

Loops in CF: To loop or not to loop?

Description:

HTML list boxes of hours that goes from 0 to 23 SELECT NAME='Hour' CFOUTPUT ... The following code reads a text file, then loops through the content of the file. ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 34
Provided by: michael118
Category:
Tags: codes | com | html | list | loop | loops | messages | of | read | text | true

less

Transcript and Presenter's Notes

Title: Loops in CF: To loop or not to loop?


1
Loops in CFTo loop or not to loop?
  • Neil Ross
  • www.codesweeper.com
  • neil_at_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
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
  • ltCFLOOP
  • index"parameter_name from"beginning_value"
  • to"ending_value"STEP"increment"gt
  • Lines to repeat
  • lt/CFLOOPgt

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
  • ltCFOUTPUTgt ltCFLOOP index"LoopCount" from"5"
    TO"1" step"-1"gt
  • The loop index is LoopCount.ltBRgt
  • lt/CFLOOPgt
  • lt/CFOUTPUTgt
  • 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
  • ltSELECT NAME"Hour"gt
  • ltCFOUTPUTgt
  • ltCFLOOP INDEX"hour" FROM"0" TO"23"gt
  •    ltOPTION VALUE"hour"gthour
  • lt/CFLOOPgt
  • lt/CFOUTPUTgt
  • lt/SELECTgt

11
Nested Loop Example
  • List box with all the hours and minutes of the
    day.  
  • ltSELECT NAME"HourAndMinutes"gt
  • ltCFOUTPUTgt
  • ltCFLOOP INDEX"hour" FROM"0" TO"23"gt
  •   ltCFLOOP INDEX"minute" FROM"0" TO"59"gt
  •         ltOPTION VALUE"'hourminute'"gthour
    minute
  •   lt/CFLOOPgt
  • lt/CFLOOPgt
  • ltCFOUTPUTgt
  • lt/SELECTgt

12
WHILE Loops
  • DO WHILE loop
  • Same syntax as CFIF conditional logicltCFSET
    Dice 0gtltCFLOOP CONDITION"Dice LTE 5"gt
    ltCFSET Dice RandRange(1,6)gtltCFOUTPUTgtdicelt/CF
    OUTPUTgtltBRgt
  • lt/CFLOOPgt

13
WHILE Loop Details
  • FOR and LIST loops are executed a certain number
    of times
  • WHILE loops are executed while a condition is
    true
  • ltCFLOOP CONDITIONwhile-conditiongt
  • Statements to loop through
  • lt/CFLOOPgt

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
  • (dont use gt etc because CFML uses gt)
  • 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
  • ltCFSET StopIt 0gt
  • ltCFLOOP CONDITIONTRUEgt
  • ltCFSET StopIt RandRange(1,10)gt
  • ltCFIF StopIt LTE 5gt
  • ltCFBREAKgt
  • lt/CFIFgt
  • ltCFOUTPUTgtStopItlt/CFOUTPUTgtltBRgt
  • lt/CFLOOPgt
  • 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
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email FROM Customer
  • lt/CFQUERYgt
  • ltCFOUTPUT QUERY"GetEmail"gt
  • GetEmail.EmailltBRgt
  • lt/CFOUTPUTgt
  • Variable available
  • Queryname.currentrow
  • Queryname.recordcount

20
CFLOOP Query Loop
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email FROM Customer
  • lt/CFQUERYgt
  • ltCFOUTPUTgt
  • ltCFLOOP QUERY"GetEmail"gt
  • GetEmail.EmailltBRgt
  • lt/CFLOOPgt
  • lt/CFOUTPUTgt

21
CFMAIL loop
  •  Send one email for each record in the query 
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email FROM Customer
  • lt/CFQUERYgt
  • ltCFMAIL QUERY"GetEmail"
  •       TO"GetEmail.Email"
  •       FROM"info_at_mycompany.com"
  •       SUBJECTTest
  •       SERVER"smtp.mycompany.com"gt
  • Hi There
  •    lt/CFMAILgt

22
Nested Query Loop Example
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email , SecurityLevel  FROM Customer
  • lt/CFQUERYgt
  • ltCFLOOP QUERY"GetEmail"gt
  •    ltCFQUERY NAME"GetText" DATASOURCE"Library"gt
  •       SELECT EmailText, EmailSubject FROM
    Messages
  •       WHERE  SecurityLevel GetEmail.SecurityLev
    el
  •    lt/CFQUERYgt
  •     ltCFMAIL QUERY"GetText"
  •       TO"GetEmail.Email"
  •       FROM"info_at_mycompany.com"
  •       SUBJECT"GetText.EmailSubject"
  •       SERVER"smtp.mycompany.com"gtGetText.EmailTe
    xt
  •    lt/CFMAILgt
  • lt/CFLOOPgt

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
  • ltCFOUTPUTgtltCFLOOP INDEX"ListElement"
  • LIST"form.state"
  • DELIMITERS","gt
  • ListElementltBRgt
  • lt/CFLOOPgt
  • lt/CFOUTPUTgt
  • 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
  • ltCFLOOP INDEXlist_variable
    LISTvalue_listgt
  • Statements to loop through
  • lt/CFLOOPgt

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
  • ltCFLOOP INDEXStateName
  • LISTMD, VA, DCgt
  • ltCFOUTPUTgtStateName
  • lt/CFOUTPUTgtltBRgt
  • lt/CFLOOPgt
  • 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.
  • ltcffile action"READ" file"C\afile.txt"
    variable"text_file"gt
  • ltcfoutputgt
  • ltCFLOOP list"text_file" index"line"
    delimiters"CHR(13)"gt
  • lineltbrgt
  • lt/CFLOOPgt
  • Count lines is
  • ListLen(text_file,"CHR(13)")
  • lt/CFOUTPUTgt

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_at_codesweeper.co
    m
  • Thanks to Sandy Clarkwww.shayna.com
Write a Comment
User Comments (0)
About PowerShow.com