System information
Exercise 1: Enabling users to browse trip details 125
Limiting the number of result rows
Each of the SQL statements in the preceding table returns a result set of trips rows. The result set
can range
from zero to any number of rows. The Navigation Action page must limit the result set
count to 1, because only the initial row in the result set appears on the Trip Detail page.
ColdFusion provides the
maxRows attribute for the cfquery tag for this purpose. This attribute
limits the number of result rows returned from the database. To show only a single row at a time
in the Trip Detail page, set
maxRows to 1.
To build the Navigation Action page:
1.
Create a blank file.
2.
Enter the following code in the blank file:
<!--- NAVIGATION BUTTONS --->
<cfquery name="TripQuery" datasource="CompassTravel" maxrows="1">
SELECT tripID FROM trips
<cfif IsDefined("Form.btnPrev.X")>
WHERE tripID < #Form.RecordID#
ORDER BY tripID DESC
<cfelseif IsDefined("Form.btnNext.X")>
WHERE tripID > #Form.RecordID#
ORDER BY tripID
<cfelseif IsDefined("Form.btnFirst.X")>
ORDER BY tripID
<cfelseif IsDefined("Form.btnLast.X")>
WHERE tripID > #Form.RecordID#
ORDER BY tripID DESC
</cfif>
</cfquery>
<cfif TripQuery.RecordCount is 1>
<cflocation url="tripdetail.cfm?ID=#TripQuery.tripID#">
<cfelse>
<cflocation url="tripdetail.cfm?ID=#Form.RecordID#">
</cfif>
3.
Save the file as navigationaction.cfm in the my_app directory.
Note: In previous lessons, you adhered to good coding practices by putting queries in ColdFusion
components. To optimize performance, and because the Navigation Action page contains only a
query, the page is a ColdFusion page rather than a CFC. For more information about code reuse, see
Chapter 8, “Creating ColdFusion Elements” in ColdFusion MX Developer’s Guide.