ColdFusion Day 39:

================

 

CREATING ADDITIONAL TYPES OF LOOPS

 

So far you've learned about index loops. The CFLOOP tag, however, can be used to create several other types of loops. These include:

 

    *    Conditional loops

    *    Query loops

    *    List loops

 

CONDITIONAL LOOPS

 

Conditional loops are designed to repeat as long as a specific condition is true and to end when the condition becomes false. This corresponds to the while loops found in many programming languages. Even if you haven't used other programming environments, understanding the basic structure of the while loop makes it easy to understand the concept of a conditional loop.

 

The basic structure of a while loop is:

 

    While a condition is true

        Actions to perform

    End of loop

 

What happens here is that before the loop even executes, the condition is checked. If it is false, the loop is skipped. If it is true, the actions inside the loop are performed once. The condition is then checked again and if true, the actions are repeated. This process continues until the condition is false.

 

Using the CFLOOP tag to create a conditional loop requires the use of only one attribute: CONDITION. This attribute specifies the condition to be tested for each iteration of the loop.

 

To understand how index loops and conditional loops compare, let's implement our times table code as a conditional loop. Our original index loop was as follows:

 

    <CFSET TABLE=11>

    <CFSET ENTRIES=15>

    <CFLOOP INDEX="X" FROM=1 TO=#ENTRIES#>

        <CFSET Y = TABLE * X>

        <CFOUTPUT>#TABLE# * #X# = #Y#<BR></CFOUTPUT>

    </CFLOOP> 

 

To translate this to a conditional loop, you need to consider the condition. The condition needs to test the value of the X variable to see if it is out of range. For instance, you can set X to 1 before the loop and then use the condition X greater than Entries. This suggests a loop that looks like this:

 

    <CFSET TABLE=11>

    <CFSET ENTRIES=15>

    <CFSET X=1>

    <CFLOOP INDEX="X" FROM=1 TO=#ENTRIES#>

        <CFSET Y = TABLE * X>

        <CFOUTPUT>#TABLE# * #X# = #Y#<BR></CFOUTPUT>

    </CFLOOP>

 

At first glance, this loop looks good. However, it has a major flaw: the value of X never changes. This means that the condition will always be true and the loop will repeat forever, creating an endless loop. Endless loops are the bane of programming because they create programs, scripts, or templates that never end and may eventually consume all the memory in a computer, causing it to crash.

 

This means you need to add an additional step to the loop: incrementing the value stored in the variable X:

 

    <CFSET TABLE=11>

    <CFSET ENTRIES=15>

    <CFSET X=1>

    <CFLOOP CONDITION="X greater then Entries">

        <CFSET Y = TABLE * X>

        <CFOUTPUT>#TABLE# * #X# = #Y#<BR></CFOUTPUT>

        <CFSET X=X+1>

    </CFLOOP>

 

This code sets the value of X before the loop and then increment it inside the loop, it highlights the major difference between index and conditional loops: Conditional loops do not have indexes. To use a conditional loop to count requires manually adding code to perform the counting.

 

QUERY LOOPS

 

The idea behind a query loop should already be familiar. Chapter 3, "Getting Data from a Database," showed how to use CFOUTPUT to loop over a query result from a database and display the results.

 

CFOUTPUT provides a limited way to loop over a query result, however, inside a CFOUTPUT loop only a small range of tags can be used. This constrains the use of CFOUTPUT in performing complex tasks on a query result set.

 

The CFLOOP tag, however, can be used to loop through a query result set and perform any valid ColdFusion operations on the information, including outputting result data just as with the CFOUTPUT tag.

 

When using CFLOOP to loop over a query, the following attributes are available:

 

    QUERY    The name of the query to use for the loop.

    STARTROW    The row in the query result to start the loop at. By default, this value is 1.

    ENDROW    The row in the query result to end the loop at. By default, this value is the last row in the query result.

 

To get a sense of how to use the query result, you will work against a simple Microsoft Access database containing a list of names and phone numbers. The database contains a single table called Contacts with two fields: Name and Phone. In all examples, assume the following ColdFusion query has been performed:

 

    <CFQUERY NAME="Contacts" DATASOURCE="ContactList">

        SELECT *

        FROM Contacts

        ORDER BY Name

    </CFQUERY>

 

As you already know, it would be simple to output the results using CFOUTPUT:

 

    <CFOUTPUT QUERY="Contacts">

        #Name#:   #Phone#<BR>

    </CFOUTPUT>

 

The CFLOOP tag can be used to produce similar results:

 

    <CFLOOP QUERY="Contacts">

        <CFOUTPUT>#Name#:   #Phone#<BR></CFOUTPUT>

    </CFLOOP>

 

When using the CFLOOP tag, you are free to use any ColdFusion tags and functions that you want to within the loop. For instance, within the CFOUTPUT tag, it isn't possible to use the CFINCLUDE tag to include outside code in each iteration of the loop. This can be done with the CFLOOP tag.

 

LIST LOOPS

 

You will learn more about lists in Chapter 13, "Working with ColdFusion data structures." For now, you need to understand that a list is a series of items (numbers or strings, usually) separated by a common delimiter such as a comma, a colon, or a special series of characters (such as -+-).

 

For instance, the following are both examples of lists:

 

John, Barbara, Lucy, Pete, William

987:345:235:87:1

 

In both cases, the lists have 5 elements. In the first list the delimiter is a comma; in the second the delimiter is a colon.

 

Using CFLOOP, you can iterate through a list, repeating the loop once for each item in the list. List loops resemble index loops in that each item in the list is assigned to an index variable in each iteration of the loop.

 

When creating a list loop, the following attributes are available in the CFLOOP tag.

 

    INDEX    The name of the index variable for the list.

    LIST    The list that is to be used for the loop.

    DELIMITER    The character or characters that act as the delimiter between items in the list. This character is assumed to be a comma.

 

To see how this works, let's perform simple loops to output the two preceding list examples. First, you will output the list of names:

 

    <CFLOOP INDEX="Item" LIST="John, Barbara, Lucy, Pete, William">

        </CFOUTPUT>#Item#<BR></CFOUTPUT>

    </CFLOOP>

 

Notice that you can use the index variable inside the loop just as you did with the index loops.

 

Similarly, you can output the list of numbers as follows:

 

    <CFLOOP INDEX="Item" LIST="987:345:235:87:1" DELIMITER=":">

        </CFOUTPUT>#Item#<BR></CFOUTPUT>

    </CFLOOP>

 

The critical difference here is that you have specified the delimiter with the DELIMITER attribute. However, specifying the list and delimiter as literal values can be a constraint. You can add flexibility by using variables to specify the list and delimiter:

 

    <CFSET LIST="987:345:235:87:1">

    <CFSET DELIMITER=":">

    <CFLOOP INDEX="Item" LIST=#list# DELIMITER=#Delimiter#>

        </CFOUTPUT>#Item#<BR></CFOUTPUT>

    </CFLOOP>

 

With this structure, the list and delimiter variables can be created in any way, including by using CFSET , from a query, or from the user who provides them through a form.

 

OTHER TYPES OF LOOPS

 

Two other types of loops can be created using CFLOOP:    COM/DCOM loops and Structure loops.  COM/DCOM loops will be outlined in Chapter 30, "Including External Objects," and structure loops will be considered in Chapter 13.

 

Back   Next