Returning Results to the User

When you build search interfaces, keep in mind that there won't always be a record returned. If there is at least one record returned from a query, you will usually format that data using an HTML table. But to make sure that a search has retrieved records, you will need to test if any records have been returned using the recordcount variable in a conditional logic expression in order to display search results appropriately to users.

Usage example

For example, this code would be placed after the query to:

Note To return search results to users:
  1. Return to SearchAction.cfm in HomeSite.
  2. Directly under the closing CFQUERY tag, add a page heading:
  3. <H4>Employee Search Results</H4>
    
  4. Begin a conditional logic expression to test whether no records are returned from the query. If the test is true, return a message to the user:
  5. <CFIF GetEmployees.RecordCount IS "0">
    No records match your search criteria. <BR>
    Please click searh button and try again.
    
  6. Add a false procedure to the conditional logic expression that outputs the query results to the user, formatted in a table:
  7. <TABLE CELLSPACING="2" CELLPADDING="2" WIDTH="95%">
    <CFOUTPUT QUERY="GetEmployees">
    <TR>
        <TD>#GetEmployees.FirstName#</TD>
        <TD>#GetEmployees.LastName#</TD>
        <TD>#GetEmployees.Department_Name#</TD>
        <TD>#DateFormat(GetEmployees.StartDate)#</TD>
        <TD>#DollarFormat(GetEmployees.Salary)#</TD>
        <TD><CFIF Contract IS "Yes">
            Contract
            <CFELSE>Permanent</CFIF></TD>
    </TR>
    </CFOUTPUT>
    </TABLE>
    
  8. Add table headers directly above the begin CFOUTPUT tag:
  9. <TR>
        <TH ALIGN="LEFT">First Name</TH>
        <TH ALIGN="LEFT">Last Name</TH>
        <TH ALIGN="LEFT">Department</TH>
        <TH ALIGN="LEFT">Start Date</TH>
        <TH ALIGN="LEFT">Salary</TH>
        <TH ALIGN="LEFT">Status</TH>
    </TR>
    
  10. End the conditional logic expression after the end TABLE tag:
  11. </CFIF>
    
  12. Save the page.
  13. View EmpList.cfm in a browser.
  14. Click on the SEARCH hyperlink in the toolbar.
  15. Enter search criteria and SUBMIT the form.
  16. The SELECT statement on SearchAction.cfm returns different results based on the criteria that you submit.

Click here to run a search from SearchForm.cfm.

Click here to see SearchAction.cfm's code.

Before beginning the next chapter, move on to the summary to review development considerations.