home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Looping Over an Index When looping over an index variable, you need to specify the name of the variable, the initial value of the variable, and the terminating value of the variable. This is done in the <CFLOOP> tag with the INDEX, FROM, and TO attributes, respectively. Each of these attributes is required when looping over an index. <CFLOOP> can also take the optional STEP attribute, which you can use to specify the value by which the index variable should change after each pass through the loop. The default value of STEP is 1, which means that the loop index is increased by 1 after each iteration.

As a simple example of index-based looping, consider the following code:

<CFIF (#Form.value# GE 1) AND (#Form.value# LE 9)>
   <CFLOOP INDEX=“count” FROM=“1” TO=“#Form.value#”>
      <CFOUTPUT>#count#. #planets[count]#<BR></CFOUTPUT>
   <CFLOOP>
</CFIF>

This code takes a value specified by the user through an HTML form and checks to make sure that the value is between 1 and 9. If it is, then a loop is used to print out the elements of the array called planets, beginning with the first element and ending with the element the user specified through the form. If a user entered the value 6 on the form, ColdFusion would generate the output you see in Figure 34.18.


FIGURE 34.18  Index-based loops are useful for referencing arrays.

Looping While a Condition is TRUE You can also loop based on the value of one of the ColdFusion decision operators by using the CONDITION attribute of the <CFLOOP> tag. As long as the condition is TRUE, the loop will continue to iterate. When the condition becomes FALSE, the loop will terminate.

The following code illustrates a conditional loop:

<CFIF (IsNumeric(‘Form.value’)) AND (#Form.value# GE 1)>
   <CFSET count = 1>
   <CFLOOP CONDITION=“count LE Form.value”>
      <CFOUTPUT>#count#<BR></CFOUTPUT>
      <CFSET count = count + 1>
   </CFLOOP>
</CFIF>

This code produces the output you see in Figure 34.19. After checking to make sure that a user’s form input is a numeric value that is greater than 1, ColdFusion enters a loop and prints out the values between 1 and the value entered by the user.


FIGURE 34.19  Conditional looping enables you to repeat a set of CFML or HTML instructs as long as the looping condition is TRUE.


NOTE:  Remember that when you use conditional looping, it’s up to you to manage the variables in the condition so that it eventually evaluates to FALSE and exits the loop.

Looping Over Query Results Looping over a query result set is a useful capability in many settings. You’ve already seen how you can use <CFOUTPUT> with the QUERY attribute to loop over and print out a query result set. The example shown here will also use a set of query results, but in a different way.

Suppose your company assigns sales leads to representatives on your sales force through a ColdFusion-based lead management system. If you were coding a page for the lead administrator to make the assignment, you would most likely include a drop-down list of the representatives’ names so that the administrator could select one from the list and then click a submit button to make the assignment. Although you could hard-code all the representatives’ names into the drop-down list, a far more elegant way to do this is with a query to pull all the representatives’ names out of a database table and then loop over that result set to create the HTML code that produces the drop-down list.

In addition to being programmatically more efficient, this approach also has a maintenance advantage: If an existing representative leaves or a new representative joins the company, all that has to be done is to enter him into the database table that contains the representative information. Because ColdFusion is dynamically generating the drop-down list based on the query results, you are assured that any departed representatives will no longer show up and that any new representatives will appear as options in the list. Best of all, you will be spared from having to go through your forms to manually edit HTML code.

To see how you accomplish this, you first need to do a query that pulls the representative information:

<CFQUERY DATASOURCE=“employees” NAME=“GetReps”>
SELECT Rep_ID, Rep_Last, Rep_First
FROM Reps
ORDER BY Rep_Last, Rep_First
</CFQUERY>

The ORDER BY clause orders the query results according to the database column or columns you specify. In this case, the results are sorted alphabetically by the representative’s last name and, in the event of the same last name, the representative’s first name.

With the query results in hand, you can now dynamically generate the drop-down list as follows:

<FORM ACTION=“assignrep.cfm” METHOD=“POST”>
Please select a rep to be responsible for this lead:<P>
<SELECT NAME=“rep”>
<CFLOOP QUERY=“GetReps”>
   <CFOUTPUT><OPTION VALUE=#Rep_ID#>#Rep_Last#, #Rep_First#</CFOUTPUT>
</CFLOOP>
</SELECT>
<INPUT TYPE=“SUBMIT” VALUE=“Assign a Rep”>
</FORM>

The <CFLOOP> used here loops over each result in the query, plucking off the values of Rep_ID, Rep_First, and Rep_Last as it goes. During each pass through the loop, ColdFusion uses these values to construct an <OPTION> tag for the representative. The result is an alphabetized drop-down list with one entry for each sales representative—all generated by just a few lines of code.


NOTE:  When looping over a query result set, you don’t necessarily have to loop over the entire set. You can use the STARTROW and ENDROW attributes of the <CFLOOP> tag to specify starting and ending rows for the looping.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.