You should now be able to create a fully database integrated ColdFusion website. This section will teach you some ColdFusion coding tips you'll find useful as you make your site.
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.
<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 easily using only a few lines of code:
<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.