Working with Loops

I l @ ve RuBoard

Working with Loops

There will be times when you need to execute the same piece of code over and over again. For example, you might need to perform frequent mathematical calculations, analyze a list, or repeat a query. This is called looping. A loop does exactly what it sounds likeit loops over a piece of code until a condition is met, or for however long was specified. For example, you could add up the total cost of several products by looping over the values and adding them together, and when that was done, you could end the loop and output the value.

ColdFusion provides a <cfloop> tag to handle all of your looping requirements. You can use <cfloop> to loop over code a certain number of times by specifying "to" and "from" values. For example, you could loop from 1 to 100, causing your loop to execute 100 times. Or you can loop over a list of items. A loop that goes on forever is called an infinite loop, and is not a good thing.

You are going to use a loop to augment the form you developed in Lesson 4. If you recall the task, you had set up a field to allow a user to sign up to be on a mailing list. Now when a customer joins the mailing list, he can indicate what product categories he's interested in as well.

  1. Open X:\Lesson7\Start\basicform.cfm.

    X is the letter that corresponds to your CD drive.

    This is the completed version of the form that you worked on in Lesson 4.

  2. Add the following lines to the form:

     Please check your interests:<BR>  <input type="checkbox" name="interests" value="Wireless Products">Wireless Products<br> <input type="checkbox" name="interests" value="Wireless Services">Wireless Services<br> <input type="checkbox" name="interests" value="Wireless News">Wireless News<br> <input type="checkbox" name="interests" value="Other Wireless Info">Other Wireless Info<br><br> 

    This sets up the four check boxes for the user's interests. The categories a user will be able to choose from are Wireless Products, Wireless Services, Wireless News, and Other Wireless Info.

  3. Save the basicform.cfm file to the Lesson7\Start folder in your Inetpub\ wwwroot directory.

    You should already have the images and style sheet saved to this folder.

  4. Open a Web browser and type the following address into the location window: http://127.0.0.1/Lesson7/Start/basicform.cfm .

    You should now see the additional check boxes from which you can collect user preferences. You don't need to make any more changes to this file to support the new check boxes.

  5. Open X:\Lesson7\Start\formoutput.cfm.

    This file is identical to the one you worked with in Lesson 4 as well.

  6. At the top of the page, add the following code:

     <cfparam name="form.interests" default="None"> 

    Just like the mailinglist variable from Lesson 4, if none of the boxes are checked, then the variable "interests" will not exist. By adding the <cfparam> tag you are telling ColdFusion that if the variable "interests" does not exist, the program should create it and give it a value of None.

  7. After the Join Mailings line of the < cfmail > tag add:

     This Person is interested in:<br> 

    Now you are ready to loop over the list of items that the user checked. When you have more than one check box with the same name, as in this example, all of the values are returned within the same variable, separated by commas. That's all a list variable isa string with multiple values separated by a delimiter (in this case, a comma). You will continue working with lists in the next lesson.

  8. On the next line add :

     <cfloop list="#form.interests#"          index="currentinterest"         delimiters=","> 

    You are setting up the <cfloop> tag and telling it to loop over the list called form.interests, which contains the values from the checkboxes. The index attribute specifies the name of the variable that will hold the current value in the loop. Delimiters specifies the common separator for the list. ColdFusion was programmed to use commas as its default delimiters, but you can use the delimiters attribute to specify any other character you might want.

    Now that you have your list of values, you are ready to start looping over them.

  9. Inside the loop, add:

     #currentinterest#<br> 

    This is the piece of code you want executed several times. The currentinterest variable will be output for each value in the list. This variable contains the value from the check box. If there were three check boxes checked, then this variable would be output three times.

  10. Add the closing </cfloop> tag.

    Your loop is now complete. However, if you were to run this code now, you would only see the loop at work in the email, not in the confirmation. So of course you have to add the information to the confirmation text, in order for the user to receive feed back on what interests they selected.

  11. After the Join Mailing List confirmation add the following:

     The interests you selected were <br>  <cfloop list="#form.interests#"         index="currentinterest"         delimiters=",">         #currentinterest#<br> </cfloop> 

    The code for the loop that outputs to the page and the code for the loop in the <cfmail> tag are identical, except for the header.

  12. Save the formoutput.cfm file to the Lesson7/Start folder in your Inetpub\wwwroot directory.

    Now let's try it out.

  13. Open a Web browser and type the following address into the location window: http://127.0.0.1/Lesson7/Start/basicform.cfm .

    Fill in your information. Don't forget to select your interests!

  14. Click the Send Comments button.

    You can see that the three values selected now show up in the confirmation message after the form is submitted.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net