![]() ![]() ![]() |
||
|
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. To continue the good coding practice of separating business logic and presentation, you put the code to build the query using dynamic SQL in a function in the CFC that you have been working with.
cfcomponent
tag.
<cffunction name="getTripsFromForm" access="public" returntype="query"> <cfquery name="TripResult" datasource="CompassTravel"> SELECT tripID, tripName, tripLocation, departureDate, returnDate, price FROM trips </cfquery> <cfreturn TripResult> </cffunction>
<cffunction name="getTripsFromForm" access="public" returntype="query"><!--- Create WHERE clause from data entered via 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>
<cfquery name="TripResult" datasource="CompassTravel"> SELECT tripID, tripName, tripLocation, departureDate, returnDate, price FROM trips </cfquery> <cfreturn TripResult> </cffunction>
cfquery
block to use the dynamically built WHERE clause in the query:
<cfquery name="TripResult" datasource="CompassTravel">
SELECT tripID, tripName, tripLocation, departureDate,
returnDate, price FROM trips
WHERE #PreserveSingleQuotes(WhereClause)#
</cfquery>
You can use the Dreamweaver Extensions to create the CFC query.
|
||
![]() ![]() ![]() |