|
Jump Start To ColdFusion MX:
page 2
By: Clint Tredway
Published: 24th Sep 2000
Showing The Results Of A Query
Ok, now
that we know how to query a
database, let's see how we can show
the results of that query. Remember
the <cfoutput> tag we talked about
earlier in this article? This tag
will loop through a recordset when
the query attribute is used. Here's
how to set up the output of our
query from earlier:
<cfoutput
query="myQuery">
#col1# #col2#<br>
</cfoutput>
That's all you have to do to output
a result set from a query -- It's
that simple! Any HTML tag can be
placed inside a <cfoutput> tag as
well. For example:
<cfoutput
query="myQuery">
<table width="100%" cellpadding="0"
cellspacing="0" border="0">
<tr>
<td>#col1#</td>
<td>#col2#</td>
<tr>
</table>
</cfoutput>
The <cfoutput> tag is a very
powerful tag that has many cool
features that are beyond the scope
of this article. I will compose a
more advanced article with these
features explained in depth another
day.
On the Winodws platform variables
are not case sensitive. So myquery
and myQuery refer to the same
variable. You can have the same
variable in different scopes
though. I would not recommend doing
this, but it is possible. For
instance, you can have form.myvar
and url.myvar, but this is not good
coding practice -- I just wanted to
state that it was indeed possible.
Sending Email
Sending email is a very common task
that many developers have to do in
many of the applications we build.
In ColdFusion, sending email is
very simple. Just like the other
tags that we have used previously
in this article, this tag is called
what it does; <cfmail>. The <cfmail>
tag is what allows developers to
send email through their
applications. Here is how to use
this tag:
<cfmail
from="me@me.com" to="you@you.com"
subject="This is an email from
ColdFusion">
Hello From ColdFusion MX!
</cfmail>
This will send an email to the 'to'
address with the body of 'Hello
From ColdFusion MX'. There are
other attributes that this tag can
use are bcc, cc, type (text or
HTML), query, timeout, startrow,
endrow, and many others. This is a
powerful tag but it was not design
to be a mail-sending engine --
there are third-party mail tags
that are much better than the
default <cfmail> tag but at least
you do not have to buy a
third-party tool to send mail out
of the box.
Looping In ColdFusion
The next topic that I am going to cover is
looping in ColdFusion. To loop
through just about anything in
ColdFusion, you would use the <cfloop>
tag. With this tag you can loop
through a query, a list, an array,
a structure, do for loops, and
conditional loops. I am going to
cover just the basics here. To loop
through a query use the query
attribute of <cfloop> like this:
<cfloop
query="myQuery">
HTML and CF code here
</cfloop>
Its that simple. Now, let's go
through how to loop from one number
to another and output that number:
<cfloop
from="1" to="100" index="number">
<cfoutput>#number#</cfoutput><br>
</cfloop>
This is similar to a for loop that
you would use in PHP or ASP. The
index attribute is used in other
pieces of code like output, or an
if statement. Here’s how to loop
through a list:
<cfset
myList="1,2,3,4,5,6,7,8,9">
<cfloop list="#myList#" index="listItem">
<cfoutput>#listItem#</cfoutput>
</cfloop>
There is an optional attribute –
delimiter -- that allows you to
specify the delimiter for a given
list. The default delimiter is a
comma. Now, to loop over a
structure you would use the
collection and item attributes like
this:
<cfset
products = StructNew()>
<cfset val = StructInsert(products,
"Appliances ", "Dryer ")>
<cfset val = StructInsert(products,
"Furniture ", "Couch")>
<cfset val = StructInsert(products,
"Electronics ", "Tv ")>
<cfloop collection="#products#"
item="cartitem">
#products#<br>
#StructFind(products, cartitem)#<br><br>
</cfloop>
I know that I have not covered
everything about loops, but this
gives you the basics to start using
them. In a more advanced article I
delve into loops in greater detail.
Form Handling
When a form is submitted, ColdFusion places all
of the form variables in the 'form'
scope. If I had a form field named
'username', I would access this
value like this:
#form.username#
For the record, you do not have to
use the scope to gain access to
this variable, although using the
scope is a good programming
practice. ASP and PHP developers
will be familiar with this content
for the Request and $_POST
collections respectively.
ColdFusion also places all form
values submitted into a structure.
This is very helpful when building
dynamic forms. Lets go over a
simple code example:
Place this in a page called
myname.cfm:
<form
action="test.cfm" method="post">
Enter Your Name:<input type="text"
name="myname" size="30"><br>
<input type="submit" name="go"
value="Submit Name">
</form>
Here is test.cfm:
<cfif
IsDefined('form.myname')>
Hello <cfoutput>#form.myname#</cfoutput>
<cfelse>
Please enter your name.
</cfif>
When the form is submitted the if
block will see that the variable 'form.myname'
is present and it will output
'Hello Your Name Here'. Pretty
simple isn't it?
Conditional Statements
Let’s now look at using conditional
if statements. I just used if and
an else in the previous coding
example. Using if statements in
ColdFusion is very easy. The tag
syntax is as follows:
<cfif
expression goes here>
code to execute if the if statement
is true
</cfif>
The if statements can get much more
complicated than the example I just
showed you, but you get the idea.
You can also have else's and
elseif's in there as well. Here's a
short example:
<cfif
IsDefined('form.myname')>
Hello <cfoutput>#form.myname#</cfoutput>
<cfelseif Not
IsDefined('form.myname')>
Please enter your name.
<cfelse>
Something else goes here.
</cfif>
Conclusion
Well, that is it for the first
part of this article. In part two
I’m going to cover file handling,
email, sessions, and some other
topics. I hope this gets your
interest peeked about ColdFusion!
Read more from
Clint Tredway here |
|