Loops in CF - PowerPoint PPT Presentation

Loading...

PPT – Loops in CF PowerPoint presentation | free to download - id: 64bd76-MzhiO



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Loops in CF

Description:

Title: ColdFusion Security Author: Michael Smith Last modified by: Michael Smith Created Date: 6/25/1999 5:39:46 AM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 38
Provided by: Michael2602
Category:
Tags: index | loops | search | text

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Loops in CF


1
Loops in CF
  • Michael Smith
  • President
  • TeraTech, Inc
  • ColdFusion, Database VB custom development
  • http//www.teratech.com
  • 800-447-9120

2
Introduction
  • Loops - many types
  • Many uses

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

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.

5
Loop uses
  • Repeating HTML
  • Processing text
  • Outputing queries
  • Nested Loops
  • Breaking out of loops
  • Banding report lines

6
1.1 FOR Loops
INDEX FROM
  • FOR NEXT loop
  • ltCFLOOP INDEX"parameter_name"
  • FROM"beginning_value"
  • TO"ending_value"
  • STEP"increment"gt
  • Lines to repeat
  • lt/CFLOOPgt

INDEX INDEX STEP
Is INDEX LT TO? If so loop again
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

8
FOR Loop Example 1
  • ltCFLOOP INDEX"LoopCount" FROM"5" TO"1"
    STEP"-1"gt
  • The loop index is LoopCount.ltBRgt
  • lt/CFLOOPgt
  • (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.
9
FOR Loop Example 2
  • HTML list boxes of hours that goes from 0 to 23
  • ltSELECT NAME"Hour"gt
  • ltCFLOOP INDEX"hour" FROM"0" TO"23"gt
  •    ltCFOUTPUTgt
  •     ltOPTION VALUE"hour"gthour
  •   lt/CFOUTPUTgt
  • lt/CFLOOPgt
  • lt/SELECTgt

10
Nested Loop Example
That is 1440 items! Not a good idea for a real
site
  • List box with all the hours and minutes of the
    day.  
  • ltSELECT NAME"HourAndMinutes"gt
  • ltCFLOOP INDEX"hour" FROM"0" TO"23"gt
  •   ltCFLOOP INDEX"minute" FROM"0" TO"59"gt
  •     ltCFOUTPUTgt
  •     ltOPTION VALUE"'hourminute'"gthourminu
    te
  •     lt/CFOUTPUTgt
  •   lt/CFLOOPgt
  • lt/CFLOOPgt
  • lt/SELECTgt

11
1.2 WHILE Loops
Is condition true? If so loop again
  • DO WHILE loop
  • ltCFSET StopIt 0gt
  • ltCFLOOP CONDITION"StopIt LTE 5"gt
  • ltCFSET StopIt RandRange(1,10)gt
  • ltHRgt
  • lt/CFLOOPgt

12
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

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!

14
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

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

16
CFBREAK
  • CFBREAK exits the current loop
  • ltCFSET StopIt 0gt
  • ltCFLOOP CONDITIONTRUEgt
  • ltCFSET StopIt RandRange(1,10)gt
  • ltCFIF StopIt LTE 5gt
  • ltCFBREAKgt
  • lt/CFIFgt
  • ltCFOUTPUTgtlt/CFOUTPUTgtltHRgt
  • lt/CFLOOPgt
  • More code here

17
1.3 Query Loops
  • CFOUTPUT
  • CFLOOP
  • CFMAIL

18
CFOUTPUT Query Loop
Start at first record
  • 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

Any records left? If so loop again
19
CFLOOP Query Loop
Start at first record
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email FROM Customer
  • lt/CFQUERYgt
  • ltCFLOOP QUERY"GetEmail"gt
  • ltCFOUTPUTgtGetEmail.EmailltBRgtlt/CFOUTPUTgt
  • lt/CFLOOPgt

Any records left? If so loop again
20
CFMAIL loop
Start at first record
  •  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

Any records left? If so loop again
21
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

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

23
1.4 List Loops
Start at first item in list
  • FOR EACH loop
  • ltCFLOOP INDEX"ListElement"
  • LIST"form.state"
  • DELIMITERS","gt
  • ltCFOUTPUTgtListElement
  • lt/CFOUTPUTgtltBRgt
  • lt/CFLOOPgt
  • Other delimiters than comma are allowed

Any items left in list? If so loop again
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 loop
  • ltCFLOOP INDEXlist_variable
  • LISTvalue_listgt
  • Statements to loop through
  • lt/CFLOOPgt

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

26
List Loop example
  • List local states
  • ltCFLOOP INDEXStateName
  • LISTMD, VA, DCgt
  • ltCFOUTPUTgtStateName
  • lt/CFOUTPUTgtltBRgt
  • lt/CFLOOPgt
  • Produces.

MD VA DC
27
Text file as a list
  • 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.

28
1.5 Structure Loops
Start at first item in structure
  • Loop over all people in the Department
  • ltCFLOOP COLLECTIONDepartments ITEM"person"gt
  • person,
  • StructFind(Departments, person)
  • lt/CFLOOPgt

Any items left in structure? If so loop again
29
1.6 COM Loops
  • FOR EACH OBJECT Loops
  • ltCFLOOP COLLECTIONFFUNC ITEMfile2gt
  • ltCFOUTPUTgt file2.name ltBRgt lt/CFOUTPUTgt
  • lt/CFLOOPgt

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

31
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)

32
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 ltCFBREAKgt
  • Note Still use LE etc for conditionals in
    CFSCRIPT and not the JavaScript lt

33
CFSCRIPT for
  • 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).
  • Evaluates the final-expression.
  • Loops
  • statement can be a single semicolon terminated
    statement or a statement block in curly braces.

34
for loop example
  • Assign array a(1) 1 etc
  • for(index1 index LT 10 index index 1)
    aindexindex

35
Infinite for loop example
  • Search for key using break to stop infinite loop
  • indx0
  • for( )
  • indxindx1 if(Find("key",stringsindx,1))
    WriteOutput("Found key at " indx ".ltbrgt")
    break
  • else if (indx IS ArrayLen(strings))
    WriteOutput("Exited at " indx ".ltbrgt")
    break

36
Resources
  • CFDOCS
  • Ben Forta Books
  • http//www.cfug-md.org/articles/introCF-4-Lists.cf
    m
  • http//www.houseoffusion.com

37
Questions
  • michael_at_teratech.com
About PowerShow.com