Creating Additional Types of Loops ColdFusion Help

So far, you’ve learned about index loops. Index loops are the most basic type of loop and are found in every type of programming language. The CFLOOP tag however can be used to create several other types of loops. These include the following:

• 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 as follows:

While 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-ll>
CFSET ENTRIES-lS>
<CFLOOP INDEX-‘X’ FROM-l To-‘Entr;es#>
<CFSET v – Table * X>
<CFOUTpur~’Table’ * ,X, • ‘V'<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 Xto1 before the loop and then use the condition X greater than Entries. This suggests a loop that looks like this

<CFSET TABLE-!l>
<CFSET ENTRIES-lS>
<CFSET x-is
<CFLOOP CONDITION-‘X greater than Entr;es’>
<CFSET~ – Table * X>
<CFOUTPUT>’Table’ * ,x,- ‘V'<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-ll)
<CFSET ENTRIES-1S)
<CFSET X-1>
<CFLOOP CONDITION-‘X greater than Entries’)
<CF~ET V – Table * X>
<CFOUTPUT>*Table* * *X* – *V*<BR></CFOUTPUT>
<CFSET X – X + 1>
</CFLOOP)

This need to set the value of Xbefore the loop and then increment it inside the loop 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. In Chapter 3, “Getting Data from a Database,” you saw how to use CFOUTPUTto loop over a query result from a database and display the results.

CFOUTPUTprovides a limited way to loop over a query result, however. Inside a CFOUTPUTloop, only a small range of tags can be used. This constrains the use of CFOUTPUTin performing complex tasks on a query result set.

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

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

QUERY The name of the query to use for the loop.
STARlROW 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: Nameand Phone .

In all examples you will look at assume the following Cold Fusion query has been performed:

<CFQUERY NAME=’Contacts’ DATSOURCE-‘ContactList’>
SELECT *
FROM Contacts
ORDER BYName
</CFQUERY>
As you already know, it would be simple to output the results using CFOUTPUT as
follows:
<CFOUTPUT QUERY·’Contacts’>
#Name#: #Phone#<BR>
</CFOUTPUT>
The CFLOOP tag can be used to produce similar results:
<CFLOOP QUERY·’Contacts’>
<CFOUTPUT>#Name#: #Phone#<BR></CFQUTPUT>
</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 willleam more about lists in Chapter 14, “Working with Cold Fusion 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 five 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 required 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 conuna.

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.

Posted on November 16, 2015 in Looping

Share the Story

Back to Top
Share This