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


You can expand your programming logic by using the <CFELSE> and <CFELSEIF> tags inside a <CFIF> and </CFIF> tag pair. <CFELSE> enables you to define a block of code to execute if the condition in the <CFIF> tag evaluates to FALSE. For example:

<CFQUERY DATASOURCE=“customers” NAME=“GetCustomers”>
SELECT name, phone
FROM Customers
WHERE city = ‘Springfield’
</CFQUERY>

<CFIF GetCustomers.RecordCount IS NOT 0>
   <H1>Customers Living in Springfield</H1>
   <TABLE BORDER=1>
   <TR><TH>Name</TH><TH>Phone Number</TH></TR>
   <CFOUTPUT QUERY=“GetCustomers”>
   <TR><TD>#name#</TD><TD>#phone#</TD></TR>
   </CFOUTPUT>
   </TABLE>
<CFELSE>
   <H1>No customers in Springfield</H1>
</CFIF>

After doing the query named GetCustomers, ColdFusion will check to see how many records were returned. If the number of records is not zero, ColdFusion prints out a table of the names and phone numbers of the records selected in the query. Otherwise, the number of query results was zero, so ColdFusion outputs a message saying that no customers were found.


It’s helpful to indent code inside of <CFIF>, <CFELSE>, <CFELSEIF>, and </CFIF> tags because it makes troubleshooting the code much easier.

In addition to the <CFELSE> tag, you can also place as many <CFELSEIF> tags as you’d like between the <CFIF> and </CFIF> tags. Suppose, for example, you had a string variable named residence that could take on one of four values. You could the use the following code to do conditional processing based on the four values:

<CFIF residence IS “apartment”>
   <B>Please enter your monthly rent: </B>
<CFELSEIF residence IS “condo”>
   <B>Please enter your total monthly mortgage payment + condo fee: </B>
<CFELSEIF residence IS “home”>
   <B>Please enter your total monthly mortgage payment: </B>
<CFELSEIF residence IS “other”>
   <B>Please enter your monthly housing expense: </B>
</CFIF>
<INPUT TYPE=“TEXT” NAME=“housing_expense”>

Technically, if you were absolutely sure that residence could only take on values of “apartment”, “condo”, “home”, or “other”, you could change the <CFELSEIF residence IS “other”> tag to a <CFELSE> tag because that would be the only possibility left at that point in the processing.

The <CFIF> tag is also flexible enough to work with other constructs that evaluate to either TRUE or FALSE. These include

  Compound decision operators—You can join decision operators with logical ANDs and ORs to produce more complex decision criteria. For example:
<CFIF (Sex IS “Male”) AND (Age GE 16)>
   <B>You must register with the Selective Service.</B>
</CFIF>
  It’s a good idea to group each individual decision operator in parentheses so that it’s easier to troubleshoot the expression later.
  Negation operator—You can also use the NOT operator to logically negate one of the decision operators:
<CFIF NOT(Age GE 21)>
   <B>You are not old enough to drink.</B>
</CFIF>
  Boolean variables—You can also use Boolean variables in place of a decision operator in a <CFIF> tag. This is useful when the variable is a flag in the program. In the following example, a set of names taken from a query is printed out in a comma-delimited list. Each name from the query needs a comma and a space in front of it, except for the first name printed. You can control for this by introducing the printed_one flag, which has a value of TRUE if the first item in the list has been printed.
<CFSET printed_one = FALSE>
<CFOUTPUT QUERY=“names”>
<CFIF printed_one>
   , #name#
<CFELSE>
   #name#
   <CFSET printed_one = TRUE>
</CFIF>
</CFOUTPUT>
  CFML functions—Many of the ColdFusion functions evaluate to values of TRUE or FALSE. Any of these functions is valid to use inside the <CFIF> tag. The IsNumeric function, for example, checks a variable to see whether it is a number. You can use IsNumeric together with a <CFIF> tag to do some basic data checking as follows:
<CFIF NOT(IsNumeric(‘Form.price’))>
   The price you entered is not valid.
</CFIF>

Using Looping Constructs

Looping enables you to execute a set of instructions again and again until a certain condition is met. ColdFusion loops are implemented with the <CFLOOP> tag and can be one of four types:

  Looping over an index
  Looping while a specified condition is TRUE
  Looping over a query result set
  Looping over the items in a list

Each type of loop is considered in the following five sections.


NOTE:  You can also loop over a COM/DCOM collection object, but this type of object is a bit too advanced for an introductory discussion on ColdFusion looping. For more details, consult Chapter 34, “Interfacing with COM and DCOM Objects,” in Ben Forta’s The ColdFusion Web Application Construction Kit, Third Edition.


CAUTION:  

Whenever you’re doing any kind of looping, make sure that you have a reasonable Request Timeout value set in the ColdFusion Administrator. This prevents an infinite loop from consuming all your server’s resources and crashing it.



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.