Exercise 4: Writing Structured, Reusable Code - MACROMEDIA COLFUSION MX 7-GETTING STARTED BUILDING COLDFUSION MX Getting Started

Building coldfusion mx applications
Table of Contents

Advertisement

<body>
<h1>Trip List</h1>
<cfoutput query="TripList">#tripName#<br></cfoutput>
</body>
</html>
Save the file as triplisting.cfm in the my_app directory.
2.
View the triplisting.cfm page in a browser. The page lists all the trip names retrieved from the
3.
Compass Travel database.
Reviewing the code
The following table describes the code used to display the query result set:
Code
<cfoutput query="TripResult">
#tripName#<br</cfoutput>

Exercise 4: Writing structured, reusable code

Generally, it is good coding practice to separate business logic from the display. The ColdFusion
page that you just created contains both business logic (the database query) and presentation (the
output code block). To separate them, you put the query in a ColdFusion component (CFC).
Doing so separates business logic from presentation; it also makes it easy to reuse the query
anywhere in your application. For more information, see Chapter 10, "Building and Using
ColdFusion Components" in ColdFusion MX Developer's Guide.
To move the query to a CFC:
Create the CFC file.
1.
Copy the query to the CFC.
2.
Call the method that contains the query.
3.
Creating the CFC file
ColdFusion components (CFCs) are special files saved with the filename extension .cfc. They can
contain data and functions. Within CFCs, functions are referred to as methods. Actions that you
want ColdFusion to perform, such as querying a database, are contained in component methods.
One CFC can contain many methods. Each method in a CFC can return only one variable. The
following is the general syntax of a CFC:
<cfcomponent>
<cffunction name="firstMethod">
<!--- CFML code for this method goes here. --->
</cffunction>
<cffunction name="secondMethod">
<!--- CFML code for this method goes here. --->
</cffunction>
</cfcomponent>
Explanation
Output code block. Displays the value of the tripName column
for each row in the result set from the "TripResult" query.

Exercise 4: Writing structured, reusable code

63

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLFUSION MX 7-GETTING STARTED BUILDING COLDFUSION MX and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Colfusion mx 7 - installing and using coldfusion mx

Table of Contents