Building Flexible Search Interfaces

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.

Usage example

For example, to allow users to search for employees by last name, department, or both, you would build a query that looks like this:

    SELECT  Departments.Department.Name,
    FROM Departments, Employees
    WHERE Departments.Department_ID = Employees.Department_ID
    <CFIF Form.Department_Name IS NOT "">
    AND Departments.Department_Name = 'Form.Department_Name'
Note To build a flexible search interface:
  1. Open SearchForm.cfm.
  2. Rename the title to Chapter 9 Employee Search Form.
  3. Change the FORM tag's ACTION attribute to SearchAction.cfm.
  4. Close and save the page.
  5. Create a new application page in HomeSite.
  6. Save the page as SearchAction.cfm within the CFDocs directory.
  7. Naming is important because SearchForm.cfm passes variables to SearchAction.cfm when the form is submitted.

  8. Title the page Chapter 9 Employee Search Results.
  9. Include Toolbar.cfm directly beneath the BODY tag:
  10. <CFINCLUDE TEMPLATE="Toolbar.cfm">
  11. Directly beneath the CFINCLUDE tag, create a query named GetEmployees to retrieve data using the search criteria passed from the SearchForm.cfm page:
  12. <CFQUERY NAME="GetEmployees" DATASOURCE="HRExpress">
  13. Add a SELECT statement within the query block to retrieve the FirstName, LastName, StartDate, Salary, Contract, and Department_Name columns from the Employees and Departments tables:
  14. SELECT Employees.FirstName,
    Employees.LastName, Employees.StartDate,
    Employees.Salary, Employees.Contract,
    FROM Employees, Departments
  15. Directly beneath the FROM statement, join the tables together using the common column Department_ID:
  16. WHERE Departments.Department_ID = Employees.Department_ID
  17. Add a nested CFIF block directly after the WHERE clause to test if Form.LastName is defined and if it has a working value. If both are true, add an AND clause with pattern matching to include Form.LastName as search criteria:
  18. <CFIF IsDefined("Form.LastName") IS "YES">
        <CFIF Form.LastName IS NOT "">
        AND Employees.LastName LIKE '%#Form.LastName#%'
  19. Add a nested CFIF block to the query to test if Form.Department_Name is defined. If it is, and if the user is searching on a single department rather than all departments, add an AND clause to include Form.Department_Name as search criteria:
  20. <CFIF IsDefined("Form.Department_Name") IS "YES">
        <CFIF Form.Department_Name IS NOT "ALL">
        AND Departments.Department_Name='#Form.Department_Name#'
  21. Add another CFIF block to the query to test if Form.Contract exists, if so, add an AND clause to include Form.Contract as search criteria:
  22. <CFIF IsDefined("Form.Contract") IS "YES">
    AND Employees.Contract='#Form.Contract#'
  23. Save the page.
  24. Test the search interface in your browser.
  25. 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 SearchForm.cfm.

Click here to see SearchAction.cfm's code.

Move on to the next procedure to display the search results to users.