Frequently, you will want users to optionally enter multiple search criteria.
Wrap conditional logic around the SQL AND clause to build a flexible search interface.
For example, to allow users to search for employees by last name, department, or both, you would build a query that looks like this:
<CFQUERY NAME="GetEmployees" DATASOURCE="HRApp"> SELECT Departments.Department.Name, Employees.FirstName, Employees.LastName, Employees.StartDate, Employees.Salary FROM Departments, Employees WHERE Departments.Department_ID = Employees.Department_ID <CFIF Form.Department_Name IS NOT ""> AND Departments.Department_Name = 'Form.Department_Name' </CFIF> </CFQUERY>
|To build a flexible search interface:|
Naming is important because
SearchForm.cfm passes variables to
SearchAction.cfm when the form is submitted.
<CFQUERY NAME="GetEmployees" DATASOURCE="HRExpress"> </CFQUERY>
SELECT Employees.FirstName, Employees.LastName, Employees.StartDate, Employees.Salary, Employees.Contract, Departments.Department_Name FROM Employees, Departments
WHERE Departments.Department_ID = Employees.Department_ID
<CFIF IsDefined("Form.LastName") IS "YES"> <CFIF Form.LastName IS NOT ""> AND Employees.LastName LIKE '%#Form.LastName#%' </CFIF> </CFIF>
<CFIF IsDefined("Form.Department_Name") IS "YES"> <CFIF Form.Department_Name IS NOT "ALL"> AND Departments.Department_Name='#Form.Department_Name#' </CFIF> </CFIF>
<CFIF IsDefined("Form.Contract") IS "YES"> AND Employees.Contract='#Form.Contract#' </CFIF>
The returned records will not be displayed because you have not entered that code yet, however, you will see the number of records returned if you have debugging enabled.
Click here to run a search from
Click here to see
Move on to the next procedure to display the search results to users.