|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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.
In addition to the <CFELSE> tag, you can also place as many <CFELSEIF> tags as youd 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
Using Looping ConstructsLooping 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:
Each type of loop is considered in the following five sections.
|
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. |