
ColdFusion MX 7 Getting Started Experience Tutorial Page 37 of 47
Learning Point: Conditional processing
Now that you have successfully displayed all the information from the database, it is time to improve the display. Rather
than having just one long column of artwork, it would be more efficient to be able to browse the pieces in a 3-column grid.
As you recall, doing this with static HTML would be extremely tedious, forcing you to cut and paste each piece of data to
move it to a different cell. Using the power of ColdFusion, you can make this happen in just a few lines of code.
Consider the following:
• Every time you query the database and get data returned, ColdFusion creates a special variable that numbers each
record returned. In this example, the variable is artwork.currentRow. As the cfoutput tag loops over each
record for display, you can access this special variable to determine which row is currently being processed. Keep in
mind that this record is in no way associated with any primary key from a database table.
• You want data to be displayed with three columns per row. In HTML terms, you want something like the following:
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
This means that you only want the opening tr tag to be generated by ColdFusion before the first, fourth, seventh
record, and so on, being processed, and that you only want the closing tr tag to be generated by ColdFusion after
the third, sixth, ninth record, and so on.
• ColdFusion has arithmetic operators that allow you to perform calculations. The mod operator would be useful in this
case; it takes a number, divides it by another number, and gives you the remainder after division. For example:
1 mod 3 = 1 (1 divided by 3 gives you a remainder of 1)
2 mod 3 = 2 (2 divided by 3 gives you a remainder of 2)
3 mod 3 = 0 (3 divided by 3 gives you a remainder of 0)
4 mod 3 = 1 (4 divided by 3 gives you a remainder of 1)
5 mod 3 = 2
6 mod 3 = 0
7 mod 3 = 1
8 mod 3 = 2
9 mod 3 = 0
Remember that you need the opening tr tags to be generated before the first, fourth, and seventh records. From the
preceding calculations, you can see that each one of these numbers divided by 3 gives you a remainder of 1.
Similarly, you want the closing tr tags to be generated after the third, sixth, and ninth records. You can see that each
one of these numbers divided by 3 gives you a remainder of 0.
As each record is being processed, you need to tell ColdFusion MX 7, “If the current row being displayed has a
remainder of 1 after you divide it by 3, print an opening tr tag before you create the table cell.” Likewise, you also
need to tell ColdFusion, “If the current row being displayed has a remainder of 0 after you divide it by 3, print a closing
tr tag after you create the table cell.”
• One of the ColdFusion tags that tells ColdFusion to perform different tasks based on a condition is the cfif tag; for
example:
<cfif artwork.currentRow mod 3 eq 1>
<tr>
</cfif>
Commenti su questo manuale