
The Quest for ColdFusion: Control Structures
What if...
What if I
wanted to make a decision right now? Do I go to
Albertson's, or not? If I don't go to Albertson's, what
will I do. This type of decision-making is constantly
happening in our minds everyday. What will I eat for
breakfast? What will I wear? What will I type next...?
These decisions are going on, and on, in a rapid-fire
succession that becomes almost unnoticed by our
conscience mind. These decisions are made quickly, and
efficiently by our brains, but what about our web
applications? They do not have an innate sense of
intelligence. What do I do if this is true, or if that is
true? Well, the truth is, your web app doesn't know what
to do, unless you tell it. These decision-making elements
of our web applications are called "control
structures". They are called "control
structures", because they control the
decision-making aspects of our programs.
To
begin...
In this
article, we are going to take a look at the
<CFIF>-<CFELSE> control statement. The
<CFIF>-<CFELSE> statement is considered the
easiest control structure to understand by many, and
therefore, we will begin our discussion with it.
To begin,
we need to create two html/cfm files that allow us to
demonstrate the use of the <CFIF>-<CFELSE>
statements. The first, testParam.html:
<TITLE>Welcome to the Test</TITLE>
<P><FONT SIZE=4 STYLE="font-size: 16pt">
<B>Welcome to the ColdFusion Test Application:</B></FONT></P>
<FORM ACTION="testParam2.cfm" METHOD="post">
<P><FONT SIZE=3><B> Please enter your name here:
<INPUT TYPE=TEXT NAME="name" SIZE=25></B></FONT></P>
<P><FONT SIZE=3><B> Please enter your telephone here:
<INPUT TYPE=TEXT NAME="phone" SIZE=25></B></FONT></P> <DIV ALIGN=LEFT>
<P><FONT SIZE=3><B> <INPUT TYPE=BUTTON NAME="submit" VALUE="Submit"></B></FONT></P>
</DIV></FORM></BODY></HTML>
And, the
second, testParam2.cfm:
<CFPARAM NAME="Form.likePage" default="0">
<TITLE>This page interprets the data from test.cfm </TITLE>
For those
of you who have been following the ColdFusion Quest
articles, you will recognize this code from last month's article,
"Dealing With Empty Variable Names In
ColdFusion". That is because we are steadily
building upon our knowledge from one article to the next,
and also, because the <CFIF>-<CFELSE>
statement allows us to manipulate our parameter values,
(etc, etc) in a easy 1-2-<CFIF>-<CFELSE>.
As you
may remember, we have created a form, testParam.html,
with two text boxes, name and phone, and then added a
checkbox, which asks our user if they like the page. The
results of this form are then sent forward to a page
called testParam2.cfm, which checked for the value held
in a variable, called Form.likePage. If this variable did
not exist, it created a variable Form.likePage with a
default value of "0".
Here
is the question...
What if I
wanted to make my application take a specific course of
action based upon the value of the variable
Form.likePage? Suppose I wanted to display an error
message in the case of a zero value on the previous page.
(Much like validation code.) With your
<CFIF>-<CFELSE> control statement, it is very
easy. Take a look at this:
<CFPARAM NAME="Form.likePage" default="0">
<!--- This is the <CFIF> statement. It checks for a value of "0", then reacts accordingly --->
<CFIF Form.likePage IS "0">
<meta http-equiv="refresh" content="3; url=testParam .html">
<h2><center>You have not answered our nice aesthetics question. You will be returned back to the previous page in 3 seconds</h2>
<TITLE>This page interprets the data from test.cfm </TITLE>
The if
statement by itself, will check a condition statement to
find if something is true, and then react. If the value
of Form.likePage is equal to "0", then a blank
page with the message, "You have not answered our
nice aesthetics question. You will be returned back to
the previous page in 3 seconds". This will then
return the user to the previous page within 3 seconds. If
the value of Form.likePage is something other than
"0", then the <CFIF> statement is
ignored, and then the rest of the html page is printed to
the screen with the ColdFusion variables embedded into
the page. Another way to right this same action is to add
a <CFELSE> to the code.
<CFPARAM NAME="Form.likePage" default="0">
<!--- This is a <CFIF> statement with the inclusion of the <CFELSE> tag.
It tests for a value of "0". If this is not true, it returns the HTML
page with the ColdFusion variables embedded. --->
<CFIF Form.likePage IS "0">
<meta http-equiv="refresh" content="3; url=testParam .html">
<h2><center>You have not answered our nice aesthetics question. You will be returned back to the previous page in 3 seconds</h2>
<TITLE>This page interprets the data from test.cfm </TITLE>
This
code performs the same action as the previous page,
except that it is perhaps easier to follow the flow of
control through the script. We see that we are testing to
see if Form.likePage="0", if it is not true,
then perform the appropriate default action, found after
the <CFELSE> tag. In this case, display the html,
with the ColdFusion variables, to the client browser. An
easy way to remember this is to say, "If this is
true, do this action, or else do that action."
To
expand on this...
The abilities
of the <CFIF>-<CFELSE> statement, can be
further expanded, by using <CFELSEIF>. If you need
to check for two different possibilities, then you would
write:
<CFPARAM NAME="Form.likePage" default="0">
<!--- This introduces the <CFELSEIF> tag. It allows us to check for
multiple possibilities --->
<CFIF Form.likePage IS "0">
<meta http-equiv="refresh" content="3; url=testParam .html">
<CFELSEIF Form.likePage IS "2">
<h2><center>There is no way have a value of 2 for this checkbox. You must be
modifying my code to do that ;-( </h2>
<TITLE>This page interprets the data from test.cfm </TITLE>
As you
can see, we are testing Form.likePage for a value of
"0", which would mean there was no checkmark in
the checkbox on the previous page, plus, we are also
testing for a value of "2", (which is
impossible with the code that we have). Therefore, if our
control statement does find a value of "2", we
output the following message to the screen: "There
is no way have a value of 2 for this checkbox. You must
be modifying my code to do that ;-( ". If neither of
these conditions are true, then output the html page with
the ColdFusion variables embedded.
There you
have it...
There you
have it! :) Your introduction to the possibilities of
control structures. These examples are just the
beginning. Next month, we will begin looping over our
ColdFusion code until our conditions are satisfied :)
Until then, Adios from your Dynamic, Car-Dodging Web
Administrator.
For
more information, you can contact me at: speeves@unt.edu
|