ColdFusion
  Home arrow ColdFusion arrow Introduction to ColdFusion Markup Language...
Web Buyers Guide
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
COLDFUSION

Introduction to ColdFusion Markup Language, concluded
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2006-01-26

    Table of Contents:
  • Introduction to ColdFusion Markup Language, concluded
  • Query Loop
  • Collection Loop
  • The cfdirectory tag
  • The cffile tag
  • The cfform tag

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Lose your application development headaches. Start developing and deploying applications with Advantage Database Server today. Download a 30-day trial for Free!

    Introduction to ColdFusion Markup Language, concluded
    (Page 1 of 6 )

    If you want to get a good start on learning ColdFusion, look no further. This article covers a whole bunch of CFML tags and some basic functions. It is the second of two parts, and excerpted from chapter three of the book ColdFusion Web Development with Dreamweaver MX 2004, written by Jen and Peter deHaan et al. (Apress; ISBN: 1590592379).

    <cfloop>

    Loops are typically used to iterate through a Recordset one record at a time, or when you need to execute a block of code a certain number of times. There are five different kinds of<cfloop>s: index loops, conditional loops, query loops, list loops, and collection loops. We cover all five in this section.

    Index Loops

    Index loops (also known asfor ... nextloops in other languages) are used to loop from a starting value to a predefined end value. The following snippet loops from 1 to 12:

    <cfloop index="counter" from="1" to="12">
     
    ...
    </cfloop>

    For each iteration of the loop, a variable (in this case#counter#) is set to the value between 1 and 12. For example, by using this kind of loop you can loop from 1 to the number of days in a specific month and build a simple calendar, as you will see in the next chapter. This loop is useful when you know exactly how many times you need to loop through a block of code.

    You can also specify an optional step attribute that controls how much the counter is incremented or decremented for each iteration of the loop, as you can see in the following code. By default, the step is 1, which means that the loop increments by 1 each time. For the next iteration, the#counter#variable equals 2, then 3, and so on. If we set a step of -1 or -2, then we will loop backwards by 1 or 2 on each iteration. To test this, type the following code into a new ColdFusion file (indexloop.cfm):

    <cfloop index="counter" from="12" to="1" step="-2"> 
      <cfoutput>#counter#<br /></cfoutput> </cfloop>

    If you run this code in a web browser, you'll get the output shown in Figure 3-10.


    Figure 3-10.   The numbers output by the index loop

    Conditional Loops

    Conditional loops (also known asdo ... whileloops) loop just as long as a condition is true. An example of a conditional loop is as follows (condloop.cfm):

    <cfset i = 18>
    <cfloop condition="i GTE 10">
      <cfoutput>#i#<br /></cfoutput>
      <cfset i = i - 1>
    </cfloop>

    Here we are setting an initial value of#i#to 18 and creating a conditional loop that will loop as long as#i#is greater than or equal to (GTE) 10. For each iteration of the loop, we are displaying the current value of#i#and then decrementing the value of#i#.This loop will display the numbers 18 to 10 to the screen in descending order.

    CAUTION   Conditional loops can be dangerous! If the end condition cannot be met, the loop does not stop, thus causing what is known as an infinite loop. An infinite loop can cause system instability and eat up a lot of system resources.

    Because conditional loops will loop as long as the condition evaluates to true, you can use this behavior to create a controlled infinite loop. This is useful if you don't know exactly how many times you need to loop, or need to exit the loop based on a complex condition. Let's look at an example (condloop2.cfm):

    <cfloop condition="TRUE">
      <cfset i = RandRange( 1, 10 )>  
      <cfoutput>#i#<br /></cfoutput>
      <cfif i EQ 10>
       
    <cfbreak>
      </cfif>
    </cfloop>

    By setting the loop condition toTRUE, you are essentially creating an infinite loop. This means that you must manually exit the loop because the end condition is always true. You can exit the loop by using the<cfbreak>tag.

    In this example, you are also using a function that you haven't seen yet:#RandRange()#. This function generates a random integer between two specified values. So, youre setting#i#to a random integer between 1 and 10 and outputting the value of#i#to the browser. If#i#is equal to 10, you exit the loop.

    You will find out more about#RandRange()#in Chapter 4.

    More ColdFusion Articles
    More By Apress Publishing


       · This article is an excerpt from the book "ColdFusion Web Development with...
     

    Buy this book now. This article is excerpted from chapter three of the book ColdFusion Web Development with Dreamweaver MX 2004, written by Jen and Peter deHaan et al. (Apress; ISBN: 1590592379). Check it out today at your favorite bookstore. Buy this book now.

    COLDFUSION ARTICLES

    - How to Access a SQL Anywhere Database with C...
    - CFXML: Probing XMLDOM in ColdFusion
    - Creating a Web Service with ColdFusion: the ...
    - CFAjax: What it is and How to Use it
    - Querying SQL 2000 Server from ColdFusion
    - Introduction to ColdFusion Markup Language, ...
    - Introduction to ColdFusion Markup Language
    - Databases and Dreamweaver MX 2004, concluded
    - Databases and Dreamweaver MX 2004
    - Welcome to Coldfusion MX 6.1, concluded
    - Welcome to Coldfusion MX 6.1
    - What You Must Know About ColdFusion Flow-Con...
    - What You Must Know About Operators in ColdFu...
    - Everything You Must Know About ColdFusion Va...
    - My First Application on ColdFusion MX Server


     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway