Register for EarthWeb's Million Dollar Sweepstakes!
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 a List ColdFusion lists are simply a collection of items separated by some kind of delimiter character. The default delimiter is a comma, but you can choose other delimiters as well. ColdFusion enables you to loop over the items in a list by using the <CFLOOP> tag with the LIST and INDEX attributes. LIST is set equal to the list you want to loop over. INDEX specifies the name of the “holding variable” that takes on the values of the individual list items as the loop progresses.

As a simple example of looping over a list, consider the following example:

<CFSET xfiles = “Mulder,Scully,Cigarette Smoking Man,Black Oil Aliens”>
Who is your favorite X-Files character?
<FORM ACTION=“favchar.cfm” METHOD=“POST”>
<CFLOOP LIST=“#xfiles#” INDEX=“character”>
   <CFOUTPUT>
   <INPUT TYPE=“RADIO” NAME=“favorite” VALUE=“#character#”> #character#
   </CFOUTPUT>
</CFLOOP>
<INPUT TYPE=“SUBMIT” VALUE=“Register my Favorite”>
</FORM>

This code produces a set of four radio buttons—one for each XFiles character in the list. Note how the INDEX variable character is used to populate the VALUE attribute of the <INPUT> tag as well as to produce the text that appears next to the radio button.


NOTE:  To change the list delimiter from a comma to another character, you use the ListChangeDelims function. The syntax of this function is explained in the “CFML Functions” section later in this chapter.

If you want to loop over a one-dimensional array, you can use the ArrayToList function to convert the array into a list and then loop over the list.

Breaking Out of a Loop You can break out of a loop before it would ordinarily terminate by using the <CFBREAK> tag. Suppose, for example, you were using a loop to look for the first non-zero element of a list:

<CFLOOP LIST=“0,0,4,7,0,9,0” INDEX=“value”>
   <CFIF value IS NOT 0>
      <CFSET magic = value>
      <CFBREAK>
   </CFIF>
</CFLOOP>

The first non-zero value is <CFOUTPUT>#magic#</CFOUTPUT>.

On each pass through the loop, the variable value takes on the next value of the next list item. The <CFIF> tag checks to see if the current value is non-zero. If it is, it sets magic equal to that value and then breaks out of the loop because there is no point in continuing the search. Thus, the loop will stop after its third iteration (when it encounters the value of 4) rather than looping through all seven values.


Try to use <CFBREAK> whenever you can. <CFLOOP> is somewhat notorious for being the most resource-intensive of the CFML tags, so if you can break out of the loop early, you increase the efficiency of your application.

Sending an Email Message

When you read about the ColdFusion Administrator, you learned that ColdFusion can interface with an electronic mail server to send email messages. But all you could do through the Administrator was to tell ColdFusion which mail server to use. When it comes time to actually compose and send a message, you need to use the <CFMAIL> tag. <CFMAIL> is a container tag, which means that there is a companion </CFMAIL> closing tag. You place the contents of the message you want to send between these two tags, as follows:

<CFMAIL>
... message to send ...
</CFMAIL>

The message doesn’t have to be plain text. In fact, it often includes ColdFusion variables. As long as the variable names are enclosed in pound signs, ColdFusion will replace the variable name with its value as it composes the message.

Certainly more components are part of an email than the body of the message, and ColdFusion enables you to handle these other message components through attributes of the <CFMAIL> tag. Following is a complete list of the <CFMAIL> tag’s attributes:

  TO—Set equal to the email address of the recipient of the message.
  FROM—Set equal to the name or the email address of the sender.
  SUBJECT—Specifies the subject of the message.
  CC—Set equal to a list of email addresses that should receive a copy of the message.
  QUERY—Denotes a query to use to generate the message. The query information can be used in one of two ways. You can loop over the query set and generate a separate email for each record in the set, or you can send the results of the entire query in a single message.
  STARTROW—If a QUERY attribute is given, you can start processing it in a row other than the first row by setting STARTROW equal to the row where the processing should commence.
  MAXROWS—If you’re looping over a query and want to limit the number of messages you’re sending, you can set MAXROWS equal to the maximum number of messages to send.
  GROUP—Specifies the query column by which to group records together. This is only applicable when sending an entire set of query results in a single message.
  MIMEATTACH—Set equal to the path and filename of a file to attach to the message.
  TYPE—If you know your recipients are using mail readers that are capable of parsing HTML-based email, you can set TYPE=”HTML” so that the message is sent in HTML format.


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.