64
Chapter 6: Lesson 2: Writing Your First ColdFusion Application
Exercise: building a query using SQL, cfquery, and cfoutput
Follow these steps to build a query that lists the current trips from the Compass Travel database.
To build the query:
1
Open an editor and create a new ColdFusion page (.cfm).
2
At the top of the file, enter the following code to dynamically retrieve the names of the current
trips listed in the Compass Travel database:
<cfquery name="TripResult" datasource="compasstravel">
SELECT tripName FROM trips
</cfquery>
<html>
<head>
<title>Trip Listing</TITLE>
</head>
<body>
<h1>Trip List</h1>
<cfoutput query="TripResult">#tripName#<BR></cfoutput>
</body>
</html>
3
Save the file as triplisting.cfm in the my_app directory.
4
View the triplisting.cfm page in a browser. The page lists all the trip names retrieved from
Compass Travel database.
Reviewing the code
The following table describes the code used to build the query:
Exercise: enhancing the query
In this exercise you will improve the Trip List page to make it easier for the Compass Travel
agents to locate trips. You must make the following improvements:
•
Sort the trip names in alphabetic order.
•
Display the departure date, return date, and price for each trip.
•
Develop a Budget Trip List report that identifies trips that are priced $1500 or less.
To enhance the trip listing query to meet these new requirements, you will modify the query you
created in the previous exercise.
Follow these steps to enhance the query to meet the new requirements. Display the triplisting.cfm
page in the browser after each step to ensure the corresponding requirement was met.
Code Explanation
<cfquery name="TripResult"
datasource="CompassTravel">
ColdFusion query named "TripResult". Submits any SQL
statement between the
cfquery
start and end tags to the data
source specified in the datasource attribute.
SELECT tripName FROM trips
SQL SELECT statement to retrieve all tripName(s) from the trips
table.
<cfoutput query="TripResult">
#tripName#<BR></cfoutput>
Output code block. Displays the value of the column tripName
for each row in the result set from the "TripResult" query.
Summary of Contents for COLDFUSION MX 61-GETTING STARTED BUILDING COLDFUSION...
Page 1: ...Getting Started Building ColdFusion MX Applications...
Page 6: ...6 Contents...
Page 10: ......
Page 30: ...30 Chapter 2 CFML Basics...
Page 36: ...36 Chapter 3 Database Fundamentals...
Page 48: ......
Page 76: ...76 Chapter 6 Lesson 2 Writing Your First ColdFusion Application...
Page 134: ...134 Index...