
Developing a search capability
71
For each search criterion on the Trip Search form, the code within the Trip Search Results page
must do the following:
•
Verify that the user entered data in the search criterion’s value field using the
cfif
tag; for
example
<cfif Form.tripLocationValue GT "">
•
If data was entered, construct a WHERE subclause by concatenating the following:
■
The SQL keyword AND
■
The corresponding SQL column name (in the Trip Search example, tripLocation) for the
search criterion.
■
The SQL operator equivalent of the search query operator
■
The test value entered by the user
The following code shows the creation of the WHERE subclause:
<cfif Form.tripLocationOperator EQ "EQUALS">
<cfset WhereClause = WhereClause & " AND tripLocation = '" &
form.tripLocationValue & "'" >
<cfelse>
<cfset WhereClause = WhereClause & " AND tripLocation like '" &
form.tripLocationValue & "%'" >
</cfif>
When you test for a string column within the WHERE clause of the SQL SELECT statement,
you must enclose the test value in quotation marks.
When you use a variable to construct a WHERE clause you must preserve the quotation marks so
that the database server does not return an error. To preserve the quotation marks, you must use
the ColdFusion
PreserveSingleQuotes
function. The
PreserveSingleQuotes
function
prevents ColdFusion from automatically escaping single quotation marks contained in the
variable string passed to the function.
Note:
The
cfqueryparam
tag also escapes single quotation marks. For more information, see
CFML
Reference
.
Constructing the initial Trip Search Results page
The following code shows how to construct the tripLocation SQL WHERE subclause.
Specifically, it uses a dynamic SQL SELECT statement built from parameters from the Trip
Search page to display the search results.
<!--- Create Where clause for query from data entered thru search form --->
<cfset WhereClause = " 0=0 ">
<!--- Build subclause for trip location --->
<cfif Form.tripLocationValue GT "">
<cfif Form.tripLocationOperator EQ "EQUALS">
<cfset WhereClause = WhereClause & " and tripLocation = '" &
form.tripLocationValue & "'" >
<cfelse>
<cfset WhereClause = WhereClause & " and tripLocation like '" &
form.tripLocationValue & "%'" >
</cfif>
</cfif>
<!--- Query returning search results --->
<cfquery name="TripResult" datasource="compasstravel">
SELECT tripName, tripLocation, departureDate, returnDate, price, tripID
FROM trips
WHERE #PreserveSingleQuotes(WhereClause)#
Содержание COLDFUSION MX 61-GETTING STARTED BUILDING COLDFUSION...
Страница 1: ...Getting Started Building ColdFusion MX Applications...
Страница 6: ...6 Contents...
Страница 10: ......
Страница 30: ...30 Chapter 2 CFML Basics...
Страница 36: ...36 Chapter 3 Database Fundamentals...
Страница 48: ......
Страница 76: ...76 Chapter 6 Lesson 2 Writing Your First ColdFusion Application...
Страница 134: ...134 Index...