Considering Multiple Cases
In the preceding example, you used several <cfelseif> tags to test cases for five of the seven days in the week. For such situations, ColdFusion provides you with another tag<cfswitch>that you can use to test the same conditions, but in a more structured way.
Example: Revisiting the Day-Specific Content
Listing 3.16 illustrates how to use the <cfswitch> tag to implement the same logic as in Listing 3.15.
Listing 3.16 Day-Specific Content Created with <cfswitch>
<html> <head> <title>Incorporating Day-Specific Content</title> </head> <body> <!--- Assess the seven different possible values of DayOfWeek(Now()). The expression attribute of the <cfswitch> tag is set equal to DayOfWeek(Now()) and the individual <cfcase> tags attempt to match the value of the expression. If none of the values match, the <cfdefaultcase> tag is processed. ---> <cfswitch expression="#DayOfWeek(Now())#"> <cfcase value="1"> <cfinclude template="sunday.cfm"> </cfcase> <cfcase value="2"> <cfinclude template="monday.cfm"> </cfcase> <cfcase value="3"> <cfinclude template="tuesday.cfm"> </cfcase> <cfcase value="4"> <cfinclude template="wednesday.cfm"> </cfcase> <cfcase value="5"> <cfinclude template="thursday.cfm"> </cfcase> <cfcase value="6"> <cfinclude template="friday.cfm"> </cfcase> <cfdefaultcase> <cfinclude template="saturday.cfm"> </cfdefaultcase> </cfswitch> </body> </html>
The <cfswitch> tag takes an expression attribute that is set equal to the expression that can have many possible valuesDayOfWeek(Now()), in this case. For each possible value, you construct a <cfcase> block that contains the code to be processed if the value associated with that case matches the current value of the expression. The <cfdefaultcase> kicks in if none of the values in the <cfcase> tags match the value of the expression.
TIP
As with <cfif> and <cfelseif>, you should put your most likely cases first in the <cfswitch> statement to keep processing time to a minimum.
NOTE
You'll get better performance if you implement the day-specific content logic with <cfswitch> rather than with <cfif> and several <cfelseif> tags. This is true because, with the <cfswitch> you have to evaluate the expression DayOfWeek(Now()) only once, but with the <cfif>, you may have to evaluate it many times.
Example: Redirecting Users to Different Pages
As a closing example for the chapter, consider the code in Listing 3.17, which shows you how to redirect users to specific pages based on which department they choose. You could use this code on an external Web site to direct visitors to information about the department they need to contact or on an intranet site to direct employees to online resources specific to their department.
Listing 3.17 Redirecting Users to Departmental Pages
<html> <head> <title> Redirecting to Department-Specific Pages</title> </head> <body> <!--- Use the users' choice of departments to redirect them to that department's home page. ---> <cfswitch expression="#Form.dept#"> <cfcase value="Accounting,Payables,Receivables"> <cflocation url="depts/accounting/home.cfm"> </cfcase> <cfcase value="Research,Development"> <cflocation url="depts/RandD/home.cfm"> </cfcase> <cfcase value="Production"> <cflocation url="depts/production/home.cfm"> </cfcase> <cfcase value="Information Technology"> <cflocation url="depts/IT/home.cfm"> </cfcase> <cfcase value="Sales"> <cflocation url="depts/sales/home.cfm"> </cfcase> <cfcase value="Marketing"> <cflocation url="depts/accounting/home.cfm"> </cfcase> <cfdefaultcase> <cflocation url="depts/general/home.cfm"> </cfdefaultcase> </cfswitch> </body> </html>
Note that the first two <cfcase> blocks handle multiple values. For example, the first <cfcase> redirects the user to the accounting home page if the chosen department is Accounting, Payables, or Receivables. You can consolidate multiple values into a single <cfcase> tag by separating the values with commas. Note also the <cflocation> tag, which redirects users to a new page as specified by the tag's url attribute.