Using the CFIF Tag


Using the <CFIF> Tag

The <CFIF> tag is used to set up a selection construct in ColdFusion. The syntax is as follows:

 <CFIF condition>     Action A <CFELSE>     Action B </CFIF> 

If the condition is TRUE, Action A is executed and Action B is skipped. On the other hand, if the condition is FALSE, Action B is executed and Action A is skipped.

The <CFELSE> tag is optional. For example, you can use the following syntax:

 <CFIF condition>    Action A </CFIF> 

In the preceding syntax, if the condition evaluates to FALSE, Action A is skipped.

The following example shows the use of the <CFIF> tag. If the value of the type variable is Date, the date is displayed, and if the value is Time, the time is displayed. Otherwise, both the time and date are displayed.

 <CFIF type IS "Date">      <CFOUTPUT>#DateFormat(Now())#</CFOUTPUT> <CFELSEIF type IS "Time">      <CFOUTPUT>#TimeFormat(Now())#</CFOUTPUT> <CFELSE>      <CFOUTPUT>#TimeFormat(Now())#, #DateFormat(Now())#</CFOUTPUT> </CFIF> 

You can also use nested <CFIF> tags by enclosing the <CFIF> tags within other <CFIF> tags. In this case, the condition in the outer <CFIF> tag is evaluated first.

 <CFIF Condition1>     <CFIF Condition2>         Action A     <CFELSE>         Action B     </CFIF> <CFELSE>     Action C </CFIF> 

To test a series of conditions until a TRUE condition is encountered, you can combine the <CFELSE> and <CFIF> tags by using the <CFELSIF> tag, as follows:

 <CFIF Condition1>     Action A <CFELSIF Condition2>     Action B <CFELSIF Condition2>     Action C <CFELSE>     Action D </CFIF> 

If an error occurs when evaluating a <CFIF> tag, you can use the <CFABORT> tag to stop further processing. Optionally, you can specify an error message to display.

Note

To control the processing of custom tags, use the <CFEXIT> tag.

Using the <CFSWITCH> Tag

The <CFSWITCH> tag allows you to perform an action based on values of an expression. The syntax for the <CFSWITCH> tag is as follows:

 <CFSWITCH EXPRESSION="expression">      <CFCASE VALUE="value1">          Action A      </CFCASE>      <CFCASE VALUE="value2">          Action B      </CFCASE>     <CFDEFAULTCASE>         Action C     </CFDEFAULTCASE> </CFSWITCH> 

ColdFusion processes these tags as follows:

The <CFSWITCH> tag evaluates an expression. Each <CFCASE> tag in the <CFSWITCH> tag body specifies a value or set of values. If a value matches the value determined by the expression in the <CFSWITCH> tag, the code in the body of the <CFCASE> tag is executed and the control moves out of the <CFSWITCH> tag. If none of the <CFCASE> tags match the value determined by the <CFSWITCH> tag, and the <CFSWITCH> tag body includes a <CFDEFAULTCASE> tag, the code in the <CFDEFAULTCASE> tag body is executed.

The following example shows the switch processing based on the value of the Department variable:

 <CFOUTPUT query = "GetEmployees"> <CFSWITCH expression = #Department#>      <CFCASE value = "Marketing">          #FirstName# #LastName# is in <b>Marketing</b><br><br>      </CFCASE >      <CFCASE value = "Finance">          #FirstName# #LastName# is in <b>Finance</b><br><br>      </CFCASE >      <CFCASE value = "Information">          #FirstName# #LastName# is in <b>Information</b><br><br>      </CFCASE >      <CDEFAULTCASE>          #FirstName# #LastName# is not in Marketing, Finance or Information.<br>      </CDEFAULTCASE> </CFSWITCH> </CFOUTPUT> 

Using the <CFLOOP> Tag

When you need to execute a set of program statements multiple times, use the <CFLOOP> tag. This tag loops through the tag body zero or more times based on the condition specified in the tag attributes. The <CFBREAK> tag is used to exit a loop.

The <CFLOOP> tag provides five types of loops, which are summarized in Table 2.10.

Table 2.10: Types of Loops

Loop Type

Description

Index

Loops through the body of the tag and increments a counter variable by a specified amount after each loop until the counter reaches a specified value.

Conditional

Checks a condition and runs the body of the tag if the condition is TRUE.

Query

Loops through the body of the tag once for each row in a query.

List

Loops through the body of the tag once for each entry in a list.

Collection

Loops through the body of the tag once for each key in a ColdFusion structure or item in a COM/DCOM object.

The following example displays the use of an index loop:

 <cfloop index = "LoopCount" from = 1 to = 5> The loop index is <cfoutput>#LoopCount#</cfoutput>.<br> </cfloop> 

The List loop allows you to list the values for the control variable, as given in the following syntax:

 <CFLOOP INDEX="list_variable" LIST="value_list">     Statements to loop through </CFLOOP> 

The INDEX attribute is assigned the name of the variable that controls the loop execution. The LIST attribute is assigned a list of comma-separated values. INDEX is assigned values in list, one at a time, as the loop executes.

The following example illustrates the use of a Conditional loop. First, an array variable is created and initialized to the word tinytots in the fourth entry. Then, a loop is created to check through the array until it encounters an array element containing the word tinytots or the end of the array. It then prints out the value of the Boolean variable that indicates whether it found the word tinytots and the array index at which it exited the loop. The code is as follows:

 <CFSET myArray = ArrayNew(1)> <!--- Use ArraySet to initialize the first ten elements to 123 ---> <CFSET ArraySet(myArray, 1, 10, 123)> <CFSET myArray[4] = "tinytots"> <CFSET foundit = False>  CFSET i = 0> <CFLOOP condition = "(NOT foundit) AND (i LT ArrayLen(myArray))">    <CFSET i = i + 1>    <CFIF myArray[i] IS "tinytots">     <CFSET foundit = True>    </CFIF> </CFLOOP> <CFOUTPUT> i is #i#<br> foundit is #foundit#<br> </CFOUTPUT> 

Note

In this example, the loop is infinite if you omit the <CFEST i = i + 1> statement. To end an infinite loop, you need to stop the ColdFusion application server. The <CFBREAK> tag exits the <CFLOOP> tag. The <CFIF> tag is used to exit the loop if a particular condition occurs.




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net