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
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
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)#
function. The
PreserveSingleQuotes
Developing a search capability
tag; for
cfif
function
71
Need help?
Do you have a question about the COLDFUSION MX 61-GETTING STARTED BUILDING COLDFUSION MX and is the answer not in the manual?
Questions and answers