- Implementing Conditional Logic
- Basic If-Then Rules
- Expanding to an If-Then-Else Rule
- Adding More Conditions to Your If-Then-Else Rule
- Considering Multiple Cases
- What's Next
Adding More Conditions to Your If-Then-Else Rule
Sometimes it's not enough to have a separate code block to process when the Boolean condition in a <cfif> instruction turns out to have a value of FALSE. In some instances, you might need to move on to test yet another condition rather than move directly to the "else" part of the statement. ColdFusion supports the <cfelseif> tag to help you with this task. <cfelseif> tags occur after the initial <cfif>, but before the <cfelse> tag, if one exists. You are free to use as many <cfelseif> tags as you like. As an example, consider the code that processes a form field named ssn that collects a Social Security number:
<cfif Len(Form.ssn) eq 0> <p>You must enter your Social Security number before submitting the form. Please use the Back button on your browser to go back and correct your submission.</p> <cfelseif Len(Form.ssn) neq 11> <p>Please enter your Social Security Number in the form xxx-xx-xxxx.</p> <cfelse> <p>Your Social Security Number is <cfoutput>#Form.ssn#</cfoutput>. </cfif>
The Boolean condition in the <cfif> tag checks to see whether the ssn field was left blank. If it was, the user sees a message that submission of the Social Security number is required. If the field was not left blank, processing moves to the <cfelseif> tag, which checks to make sure that the Social Security number is not made up of 11 characters (9 digits plus 2 dashes). If that condition has a value of TRUE, the user sees a message that indicates the expected format for the Social Security number. If the second condition is FALSE, processing passes to <cfelse>, where the user receives a confirmation of the Social Security number entered.
In short, you use <cfelseif> to test conditions beyond the initial one. The following examples illustrate situations in which you might need to test.
Example: Revisiting the Pizza Topping Selection Form
Earlier, you saw how to process the pizza topping selection form using <cfif> and <cfelse> to ensure grammatically correct sentences. Next, you'll expand the form processing script to respond to certain topping choices. Examine the code shown in Listing 3.14.
Listing 3.14 Expanded processtoppings.cfm Script
<html> <head> <title>Pizza Topping Processor</title> </head> <body> <!--- Assign a default value for Form.toppings in case the user did not choose any. ---> <cfparam name="Form.toppings" default=""> <!--- Form.toppings can be interpreted as a list, so use the ListLen() function to see how many toppings are in the list. ---> <cfif ListLen(Form.toppings)> <!--- If you're here, Form.toppings had a length that was greater than zero, so the user wants at least one topping. Use a second <cfif> to determine the grammatically correct choice of words. ---> <p>Your <cfif ListLen(Form.toppings) eq 1> topping is: <cfelse> toppings are: </cfif> <cfoutput>#Form.toppings#</cfoutput>.</p> <!--- Use a <cfif> with some <cfelseif> tags to add a follow-up question for certain toppings. ---> <cfif Form.toppings contains "Anchovies"> <p>Do you really want little salty fish on your pizza?</p> <cfelseif Form.toppings contains "Olives"> <p>Green or black olives?</p> <cfelseif Form.toppings contains "Onions"> <p>Shall we send you some breath mints, too?</p> </cfif> <cfelse> <!--- If you're here, Form.toppings had a length of zero, meaning the user wanted no toppings. ---> <p>Are you sure you want a plain pizza?</p> </cfif> </body> </html>
The <cfif> instruction added to the pizza topping processor will ask a follow-up question based on whether anchovies, olives, or onions were chosen as toppings. If the user picked anchovies, ColdFusion displays the question asking about little salty fish. Otherwise, if olives were chosen, the user is asked about green or black olives. If neither anchovies nor olives were selected and onions were selected, the user is offered some breath mints.
Note that only one of the messages will be displayed. The anchovies message has precedence, followed by the olives message, and then the onions message. If you want combinations of the messages to appear, you have to create a separate <cfif> instruction for each condition:
<cfif Form.toppings contains "Anchovies"> <p>Do you really want little salty fish on your pizza?</p> </cfif> <cfif Form.toppings contains "Olives"> <p>Green or black olives?</p> </cfif> <cfif Form.toppings contains "Onions"> <p>Shall we send you some breath mints, too?</p> </cfif>
Example: Creating a Day-Specific Message
Some sites rotate in content based on the day of the week. The content might be just a simple greeting, a new banner graphic, or a special being offered on the site that day. When considering days of the week, you have seven possibilities. To find out which day of the week it is currently, you can use the Now() function to get the current date and time from the server system clock and then feed that value into the DayOfWeek()function:
This expression will return a value between 1 and 7, where 1 represents Sunday, 2 represents Monday, and so on. You can use a <cfif>, together with five <cfelseif> tags and a <cfelse> tag, to determine which day it is and respond appropriately. Listing 3.15 shows you how.
Listing 3.15 Incorporating Content Specific to a Day of the Week
<html> <head> <title>Incorporating Day-Specific Content</title> </head> <body> <!--- Assess the seven different possible values of DayOfWeek(Now()). The condition in the <cfif> tags handles Sunday, the <cfelseif> tags handle Tuesday through Friday, and the <cfelse> tag covers Saturday since that would be the only possibility left at that point. ---> <cfif DayOfWeek(Now()) eq 1> <cfinclude template="sunday.cfm"> <cfelseif DayOfWeek(Now()) eq 2> <cfinclude template="monday.cfm"> <cfelseif DayOfWeek(Now()) eq 3> <cfinclude template="tuesday.cfm"> <cfelseif DayOfWeek(Now()) eq 4> <cfinclude template="wednesday.cfm"> <cfelseif DayOfWeek(Now()) eq 5> <cfinclude template="thursday.cfm"> <cfelseif DayOfWeek(Now()) eq 6> <cfinclude template="friday.cfm"> <cfelse> <cfinclude template="saturday.cfm"> </cfif> </body> </html>
Note here the use of the <cfinclude> tag, which reads in content from a separate file and dynamically incorporates it into the current script. The separate file is specified by the template attribute. <cfinclude> is ideal for helping to modularize your ColdFusion logic and your HTML content so that you can deploy it on every page but maintain it in only one place.
If one of the conditions you're testing is more likely to turn out to have a value of TRUE than the other, put that condition in the <cfif> tag. Then put your next most likely condition in the first <cfelseif> tag, and so on. This coding technique improves performance because it prevents ColdFusion from having to work its way through the conditions that are less likely to be true.
Because users can hit your site any day of the week, no day is probably more likely than any other, so the days are considered in the order they normally occur.