ColdFusion Tutorial Part II

Introduction: If you are reading this now then you should have already read Part I of this article, "ColdFusion Tutorial Part I – Database Integration" If you haven’t read Part I yet I suggest you do so as otherwise you may be unfamiliar with what I will talk about. In Part II of this article I will cover ColdFusion (CF) coding techniques. Everything I will cover will be found outside the <CFQUERY> tags, advanced coding within the <CFQUERY> tags will be found in Part III, "ColdFusion Tutorial Part III – Advanced SQL and Database Integration."

1. Dealing With Checkboxes

If you experimented with what I covered in Part I you may have run into problems when taking input from a checkbox. This is because when a checkbox is not checked no value is submitted, not even a null value, so the CF interpreter ends up looking for a value that isn’t there. Fortunately there is an easy fix for this.

Figure 1.CFPARAM  
<CFPARAM NAME = "addtolist" DEFAULT = "no">

By inserting the above line of code into your form processing page you are supplying the interpreter with a default value which fixes the problem.

2. Alternating Row Color

Often when displaying data from a database it is desired to alternate row background color for easy reading. This is achieved easy using only a few lines of code:

Figure 2. Alternating Row Color  
<STYLE TYPE="text/css">  
<!--
.row0 {background-color: green;}  
.row1 {background-color: white;}  
-->  
</STYLE>  
<TABLE>  
<CFOUTPUT QUERY="QueryName">  
<CFSET class="row#Int(QueryName.CurrentRow MOD 2)#">  
<TR class = #class#>  
<TD>#Field1#</td>  
<TD>#Field2#</td>  
</TR>  
</CFOUTPUT>  
</TABLE>

Now lets examine the code. First you have some CSS stating that class .row0 will have such and such a background color and class .row1 will have a different color. Next you see a CFOUTPUT tag which should now be familiar to you, and after that is something which may not be. The CFSET tag is used to assign values to variables. We are using it to define a variable named class, the value of class is created by evaluating the function row#Int(QueryName.CurrentRow MOD 2)#. The first bit of that function is simply the text "row" which we can ignore. Then we see a # sign which marks the beginning of a CF expression. The Int function returns an integer when passed a value, the value it is using is QueryName.CurrentRow MOD 2. What this value is saying is "What is the remainder when the current row number is divided by 2." As you know dividing anything by 2 will result in either a 1 or a 0 as the remainder, so all even rows will deliver a remainder of 0, all odd rows will deliver a remainder of 1. So for even rows the variable class will be "row0" and for odd rows it will be "row1" which will allow you to alternate row colors.

3. CFIF, CFELSE, CFELSEIF

If you’re familiar with any programming language you should be familiar with the concepts behind conditional code. I will now discuss the CF syntax for doing it. Every <CFIF> tag must have a matching </CFIF> tag, the <CFELSE> and <CFELSEIF> tags are completely optional. You may use as many <CFELSEIF> tags as needed, however you may use only one <CFELSE> tag per <CFIF> tag, and the <CFELSE> tag must always come last.

Figure 3.CFIF Example

<CFIF Login IS "True">  
Welcome.  
<CFELSEIF Login IS "False">  
Sorry, we could not log you in.  
<CFELSE>  
The was a problem processing your request,
please try again or contact the server admin.  
</CFIF>

When comparing values within the CFELSEIF and CFIF tags you may use the following operators:

IS – Login IS "True"
IS NOT – Login IS NOT "True"
CONTAINS – "KY, MI, CA, FL" CONTAINS State
DOES NOT CONTAIN – "MI, OH, CA" DOES NOT CONTAIN State
GREATER THAN – Age GREATER THAN "13"
LESS THAN – Age LESS THAN "62"
GREATER THAN OR EQUAL TO – Age GREATER THAN OR EQUAL TO "13"
LESS THAN OR EQUAL TO – Age LESS THAN OR EQUAL TO "62"

Also you can combine statements using AND or OR, such as: age LESS THAN "20" and sex is "male".

4. CFINCLUDE

To do SSI style includes using CF you should be using the <CFINCLUDE> tag.

Figure 4. CFINCLUDE

<CFINCLUDE TEMPLATE = "include.htm">

I think it is very straightforward and needs no explanation. However unlike SSI you can only use relative paths to the file to be included.

5. Cookies

A Cookie is a variable set on the clients computer, they are useful for tracking visitors, establishing logins, and a myriad of other things. To create a cookie in CF you use the <CFCOOKIE> tag. Then to read from the cookie you use code such as: #COOKIE.Name# where "name" is the name of the cookie.

Figure 5. Cookie Syntax

<CFCOOKIE NAME = "Name" Value = "Value" Expires = "Expiration Date">

An expiration date can be a definite date, such as "3/9/81", it can be a relative number of days "100" or it can be "Now" or "Never." Also in addition to the above there are some optional attributes to the CFCOOKIE tag. By including the word SECURE with no argument you specify that the cookie needs to be sent securely using SSL, if SSL is not available the cookie is not sent. By including DOMAIN (Domain = "webdevhq.com; webmasters-network.com") you can specify the domains the cookie applies to, and by including PATH you can specify the subset of the URL to which the cookie applies, when using PATH, DOMAIN is required.

Conclusion In this part of the article I have covered the most common CF coding techniques that are not applicable to the <CFQUERY> tag. In Part III I will cover advanced SQL and CF coding that directly affects your query.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.