User Guide

Using a web page to list trips 63
Displaying the query result using cfoutput
In Chapter 2, “CFML Basics,” on page 15, you learned that the ColdFusion
cfoutput tag is an
easy mechanism to display literal text and the contents of variables. Additionally, the
cfoutput
tag significantly simplifies displaying the results of queries. When used to display the results from
a query, the
cfoutput tag automatically loops through the record set for you. You simply specify
the name of the query in the QUERY attribute of the
cfoutput tag:
<cfoutput query="TripResult">
All the code between the cfoutput start and end tags is the output code block. The output code
block executes repeatedly, once for each row in the record set. However, if the query returns no
rows, ColdFusion skips the code contained in the output code block.
<cfoutput query = "xxx">
...output code block...
</cfoutput>
Displaying the column contents from the SQL statement
In CFML you surround variables with pound signs (#) to display their contents using the
cfoutput tag. You also use this approach with column names specified in the SELECT statement
of a
cfquery. For instance, when you want to display the trip names from the SQL query, you
would simply use
#tripName# within the output code block.
<cfoutput query="TripResult">
#tripname#
</cfoutput>
For additional information about using SQL with cfquery and cfoutput, see Developing
ColdFusion MX Applications.
Creating a dynamic web page
In the following exercises you will build a dynamic Trip Listing web page that is always current.
The first exercise guides you through constructing a query to retrieve information from the
database. In the second exercise, you will enhance the query to sort the query results and to
display other pertinent trip information.
For your convenience, the following figure shows the Compass Travel Trips table. You can refer
to this table to verify the names of the columns you use in the queries in the exercises.