Using Query Variables
So far, you have displayed data retrieved using database queries. But sometimes you'll need access to data about queries (and not just data within queries). For example, if you wanted to display the number of movies retrieved, where would you get that count from?To simplify this type of operation, ColdFusion includes special variables in every query. Table 10.1 lists these variables, and as you can see, RecordCount can provide the number of rows retrieved.
VARIABLE | DESCRIPTION |
---|---|
ColumnList | Names of columns in query results (comma-delimited list) |
ExecutionTime | Query execution time (in milliseconds), access as CFQUERY.ExecutionTime |
RecordCount | Number of rows in a query |
Listing 10.7. movies6.cfmUsing Query Variables
<!---
Name: movies6.cfm
Author: Ben Forta (ben@forta.com)
Description: Using query variables
Created: 12/15/04
--->
<!--- Get movie list from database --->
<cfquery name="movies" datasource="ows">
SELECT MovieTitle, PitchText,
Summary, DateInTheaters
FROM Films
ORDER BY MovieTitle
</cfquery>
<!--- Creat260 page --->
&l275>
<head>
<title>Orange Whip Studios - Movie List</title>
</head>
<body>
<!--- Start table --->
<table>
<tr>
<th colspan="2">
<font size="+2">
<cfoutput>
Movie List (#Movies.RecordCount# movies)
</cfoutput>
</font>
</th>
</tr>
<!--- loop through movies --->
<cfoutput query="movies">
<tr bgcolor="##cccccc">
<td>
<strong>#CurrentRow#: #MovieTitle#</strong>
<br>
#PitchText#
</td>
<td>
#DateFormat(DateInTheaters)#
</td>
</tr>
<tr>
<td colspan="2">
<font size="-2">#Summary#</font>
</td>
</tr>
</cfoutput>
<!--- End of movie loop --->
</table>
</body>
</html>
Figure 10.9. RecordCount can be accessed to obtain the number of rows in a query.
[View full size image]

#Movies.RecordCount# returns the number of rows retrievedin this case, 23. Like any other expression, the text Movies.RecordCount must be enclosed within number signs and must be between <cfoutput> and </cfoutput> tags. But unlike many other expressions, here the prefix Movies is required. Why? Because this code isn't within a query-driven <cfoutput> (there is no query attribute). Therefore, for ColdFusion to know which query's count you want, you must specify it.
Movie List (#Movies.RecordCount# movies)
TIP
Here the query name prefix is required because the query was not specified in the <cfoutput> loop. Within an output loop, the query name isn't required, but it can be used to prevent ambiguity (for example, if there were variables with the same names as table columns).Here you use RecordCount purely for display purposes. But as you will see later in this chapter, it can be used in other ways, too (for example, checking to see whether a query returned any data at all).Incidentally, why is Movies.RecordCount not in a <cfoutput query="Movies"> block? I'll not answer that one because the last time I explained it, I said it would be the last time I would do so. (That was your hint.)The other line of code that changed is the movie title display, which now has #CurrentRow#: in front of it. CurrentRow is another special variable, but this time it's in <cfoutput> instead of <cfquery>. Within an output loop, CurrentRow keeps a tally of the iterationsit contains 1 when the first row is processed, 2 when the second row is processed, and so on. In this example, it's used to number the movies (as seen in Figure 10.9).CurrentRow can also be used it to implement fancy formatting, for example, alternating the background color for every other row (a green paper effect) as seen in Figure 10.10. Listing 10.8 is movies7.cfm, a modified version of movies4.cfm (I used that older version as it's simpler and looks better for this example). Background color, as previously seen, is set using the bgcolor attribute, but unlike in the previous example, here the colors are being set dynamically and programmatically.
Figure 10.10. RecordCount can be used to alternate output colors.
[View full size image]

<cfif> was introduced back in Chapter 9, "CFML Basics".
<cfif CurrentRow MOD 2 IS 1>
xref
Listing 10.8. movies7.cfmImplementing Alternating Colors
Chapter 8, "Using ColdFusion", for an introduction to the <cfset> tag.Then bgcolor is then passed to the <tr> tag's bgcolor attribute so that on odd rows the <TR> tag becomes:
<!---
Name: movies7.cfm
Author: Ben Forta (ben@forta.com)
Description: Implementing alternating colors
Created: 12/15/04
--->
<!--- Get movie list from database --->
<cfquery name="movies" datasource="ows">
SELECT MovieTitle, PitchText
FROM Films
ORDER BY MovieTitle
</cfquery>
<!--- Creat260 page --->
&l275>
<head>
<title>Orange Whip Studios - Movie List</title>
</head>
<body>
<h2>Movie List</h2>
<!--- Display movie list --->
<table>
<cfoutput query="movies">
<!--- What color should this row be? --->
<cfif CurrentRow MOD 2 IS 1>
<cfset bgcolor="MediumSeaGreen">
<cfelse>
<cfset bgcolor="White">
</cfif>
<tr bgcolor="#bgcolor#">
<td>#MovieTitle#</td>
<td>#PitchText#</td>
</tr>
</cfoutput>
</table>
</body>
</html>
and on even rows it becomes:
<tr bgcolor="MediumSeaGreen">
The result is shown in Figure 10.10.
<tr bgcolor="White">
TIP
You'll notice that I named the variable in
