Implementing Conditional Logic

• Print
This chapter is from the book

Basic If-Then Rules

You use the <cfif> instruction to implement an if-then rule. A simple example follows:

```<cfif Form.age gt 29>
<p>You don't look a day over 29!</p>
</cfif>```

TIP

Indenting the code between <cfif> and </cfif> helps to make your scripts more readable.

The <cfif> instruction has both opening and closing tags, and the code you want to conditionally execute is located between the tags. The code can be HTML, CFML, or some combination of the two.

The Boolean condition that you're testing is located inside the <cfif> tag. In the preceding example, the condition is Form.age gt 29. This condition tests whether the value of the Form variable called age is greater than 29. If it is, the user sees this sentence: "You don't look a day over 29!". If, however, the value of age is 29 or less, the user does not see the message.

Note the use of gt instead of the greater than sign (>) in the expression of the condition. To see why this is necessary, look at the following code:

```<cfif Form.age > 29>
<p>You don't look a day over 29!</p>
</cfif>```

If you use the greater than sign instead of gt, you prematurely close the <cfif> tag. Because the greater than and less than signs are necessary for building tags, you can't use them to express conditions. Table 3.1 shows the different comparison operators and how to express them in your Boolean conditions.

Table 3.1 Expressions for Comparison Operators

 Operator Expressions Equality eq, equals, is Inequality neq, is not Less than less than, lt Greater than greater than, gt Less than or equal to less than or equal to, lte, le Greater than or equal to greater than or equal to, gte, ge

TIP

You can use the Boolean conjunctions and and or to build more complex conditions in your <cfif> tags, as in this example:

<cfif (Form.age ge 20) and (Form.age le 29)>

</cfif>

The parentheses around each individual condition aren't necessary, but they help make the statement more readable.

Example: Selecting Colors

For a more extensive example of the ways you can use <cfif>, consider the HTML form in Listing 3.1. This form allows a user to choose the foreground and background colors she prefers.

Listing 3.1 Color Choice Form

```<html>

<title>Color Picker</title>

<body>

<h1>Color Picker</h1>

<p>Please pick a background and foreground color below
and then click the "Submit" button.</p>

<form action="colorpage.cfm" method="post">

<b>Background:</b>
<select name="bgcolor" size="1">
<option>Black</option>
<option>Red</option>
<option>White</option>
</select>

<br>

<b>Foreground:</b>
<select name="fgcolor" size="1">
<option>Black</option>
<option>Red</option>
<option>White</option>
</select>

<br>

<input type="submit" value="Submit">

</form>

</body>
</html>```

Choosing the same foreground and background color would be a problem, so you can use <cfif> to determine whether they are the same and respond accordingly. Listing 3.2 shows one possible response.

Listing 3.2 The colorpage.cfm Script

```<!---

Use a <cfif> to see if the chosen colors are the same.

The <cfabort> tag stops the processing

--->

<cfif Form.bgcolor eq Form.fgcolor>

<h1>Color choice error!</h1>

<p>You choose the same foreground and background color!
color combination.</p>

<cfabort>

</cfif>

<html>

<title>Customized Color Page</title>

<cfoutput>
<body bgcolor="#Form.bgcolor#" text="#Form.fgcolor#">
</cfoutput>

<p>Here is your custom color combination!</p>

</body>

</html>```

Note the use of the <cfabort> tag, which tells the ColdFusion Application Server to stop processing the script. Also, note that you can use

`<cfif Form.bgcolor equals Form.fgcolor>`

or

`<cfif Form.bgcolor is Form.fgcolor>`

because both equals and is are valid ways to test for equality between Form.bgcolor and Form.fgcolor.

Example: Checking for the Existence of Form Fields

In Chapter 2, "Variables and Variable Scope," you learned how to use the <cfparam> tag to specify a default value for a variable that might not exist. This capability is particularly useful when you're processing a form with check boxes or radio buttons because such form fields are not passed to the ColdFusion server if they aren't selected. For this example, consider the HTML form in Listing 3.3, which asks users to indicate which age range they fall into.

Listing 3.3 Age-Range Specification Form

```<html>

<title>Age Range</title>

<body>

and click the "Continue" button.</p>

<form action="ageprocessor.cfm" method="post">

<input type="radio" name="age" value="0 - 17"> 0 - 17 years<br>
<input type="radio" name="age" value="18 - 34"> 18 - 34 years<br>
<input type="radio" name="age" value="35 - 49"> 35 - 49 years<br>
<input type="radio" name="age" value="50 - 64"> 50 - 64 years<br>
<input type="radio" name="age" value="65+"> 65 or more years<br>

<input type="submit" value="Continue">

</form>

</body>

</html>```

Some people shy away from providing information about their age, so it's certainly plausible that they might not select one of the radio buttons. In that case, Form.age will not be defined if you try to reference it in the processing script called ageprocessor.cfm. As an alternative to <cfparam>, you can use <cfif> together with the IsDefined() function to see whether the form field is there. IsDefined() tests whether a variable exists and returns TRUE if the variable does exist and FALSE if it does not. Listing 3.4 shows you how to use this function.

Listing 3.4 The ageprocessor.cfm Script

```<!---

You can use <cfif> with the IsDefined() function to provide
a default value for a form field that does not exist.

--->

<cfif Not IsDefined("Form.age")>

<cfset Form.age = "Not specified">

</cfif>

<html>

<title>Age Information</title>

<body>

<p>
Age range: <cfoutput>#Form.age#</cfoutput> years
</p>

</body>

</html>```

Note the use of the Not operator in the <cfif> instruction to check the case of the form field not being defined. Also, note that the variable whose existence you're testing for has to be in quotation marks when you use the IsDefined() function. Figures 3.1 and 3.2 show the resulting page if the user does and does not specify an age range, respectively.

Figure 3.1 When the user chooses an age range, the response page displays the range correctly.

Figure 3.2 If the user doesn't pick an age range, however, the response page reads somewhat awkwardly.

The message in Figure 3.2, Not specified years, is something the user could make sense of, but it really doesn't flow well. To alleviate this problem, you can add a second <cfif> to the ageprocessor.cfm script to selectively drop in the word years. In this case, you don't want to drop it in if Form.age has the value "Not specified". Listing 3.5 shows the updated code.

Listing 3.5 An Improved ageprocessor.cfm Script

```<!---

You can use <cfif> with the IsDefined() function to provide
a default value for a form field that does not exist.

--->

<cfif Not IsDefined("Form.age")>

<cfset Form.age = "Not specified">

</cfif>

<html>

<title>Age Information</title>

<body>

<!---

Use a second <cfif> to include the word "years"
only when it is necessary.

--->

<p>
Age range: <cfoutput>#Form.age#</cfoutput>
<cfif Form.age neq "Not specified">years</cfif>
</p>

</body>

</html>```

Example: Performing Substring Searches

So far, you have seen how to use comparison operators and the IsDefined() function to set up Boolean expressions in your <cfif> instructions. ColdFusion provides other mechanisms for setting up Boolean conditions as well. One is a substring search, in which you look for a smaller string inside a larger one. You do so by using the contains operator.

For an example of a substring search, consider the form presented by the code in Listing 3.6. Here, you ask a user to enter his computing skills into a large text window. This form might be part of a job hunting site that you're building.

Listing 3.6 Input Form for Computing Skills Information

```<html>

<title>Skill Set</title>

<body>

and click the "Continue" button.</p>

<form action="skillsprocessor.cfm" method="post">

<textarea name="skills" rows="6" cols="40" wrap="virtual">
</textarea>

<br>

<input type="submit" value="Continue">

</form>

</body>

</html>```

Because you're always on the lookout for new ColdFusion developers to join your team, you might scan the user's skills summary to see whether he has ColdFusion skills. You could do so by using the code in Listing 3.7.

Listing 3.7 The skillsprocessor.cfm Script

```<html>

<title>Skills Summary</title>

<body>

<cfif Form.skills contains "ColdFusion">

<p>We're glad to see you have some ColdFusion skills!
Your resume will be forwarded to our hiring manager.</p>

</cfif>

</body>

</html>```

In this example, ColdFusion will look through the value of Form.skills in an attempt to find "ColdFusion". If it does, the user sees a message saying the hiring manager will be notified about his candidacy and then see a restatement of his skills. If the user doesn't have ColdFusion skills, he simply sees his skill set confirmed.

NOTE

You also can use does not contain if you need to scan a string to see whether a specific substring is not part of it.