Programming instructions
Enhancing the Trip Maintenance application 91
The following example uses the MOD function to alternate the background color of table
rows:
<cfoutput query="tripResult">
<cfif CurrentRow Mod 2>
<cfset BackColor="White">
<cfelse>
<cfset BackColor="Yellow">
</cfif>
<tr bgcolor= "#BackColor#">
Notice that the MOD function uses a variable called CurrentRow. Notice also that
CurrentRow is not defined anywhere in tripsearchresult.cfm. The CurrentRow variable
is one of a few variables that ColdFusion automatically exposes that provide information
about the query. These
variables are often called query variables.
Query variables
In addition to using the
cfquery tag to return data from a ColdFusion data source, you
also can use it to return information about a query. To do this, the
cfquery tag creates a
query object, providing information in query variables as described in the following
table:
When a query variable is referenced within a
cfoutput block, the qualifying query_name
is assumed to be the query identified in the QUERY attribute of the
cfoutput tag and
does not need the
qualifier query_name. That is why the CurrentRow variable is
unqualified in the previous modulus code example.
For more information about using the modulus function or query variables in
ColdFusion applications, see Developing ColdFusion MX Applications with CFML.
Exercise: formatting the display
In this exercise, you format the currency and date fields in the Trip Search Results page
and the Trip Detail page. Additionally, you modify the Trip Search Results page to
alternate the background color of the result table rows.
To format the table:
1 To format the Trip Detail page dollar and date fields, open the tripdetail.cfm in the
my_app directory in your editor and make the following changes:
Variable Name Description
query_name.recordCount The number of records returned by the query.
query_name.currentRow The current row of the query being processed by cfoutput.
query_name.columnList A comma-delimited list of the query columns.
Existing code Change to
#departureDate# #dateformat(departureDate, "mm/dd/yyyy")#
#returnDate# #dateformat(returnDate, "mm/dd/yyyy")#