The ability to generate and display query data is one of the most important and flexible features of ColdFusion. The following sections further your understanding of using queries and displaying their results. Some of these tools are effective for presenting any data, not just query results.
You displayed each row of data from the Employee table, but the information was unformatted. You can use HTML tables to control the layout of information on the page. In addition, you can use CFML functions to format individual pieces of data, such as dates and numeric values.
You can use HTML tables to specify how the results of a query appear on a page. To do so, you put the cfoutput
tag inside the table tags. You can also use the HTML th
tag to put column labels in a header row. To create a row in the table for each row in the query results, put the tr
block inside the cfoutput
tag.
<html> <head> <title>Retrieving Employee Data Based on Criteia from Form</title> </head> <body> <cfquery name="GetEmployees" datasource="CompanyInfo"> SELECT FirstName, LastName, Salary FROM Employee WHERE LastName='#Form.LastName#' </cfquery> <h4>Employee Data Based on Criteria from Form</h4> <table> <tr> <th>First Name</th> <th>Last Name</th> <th>Salary</th> </tr> <cfoutput query="GetEmployees"> <tr> <td>#FirstName#</td> <td>#LastName#</td> <td>#Salary#</td> </tr> </cfoutput> </table> <br> <cfoutput>Contractor: #Form.Contractor#</cfoutput> </body> </html>
The following table describes the highlighted code and its function:
You might want to format individual data items. For example, you can format the Salary field as a monetary value.
To format the Salary using the dollar format, you use the CFML expression DollarFormat(number)
.
<td>#Salary#</td>
to
<td>#DollarFormat(Salary)#</td>
Use the SQL LIKE operator and SQL wildcard strings in a SQL WHERE clause when you want to compare a value against a character string field so that the query returns database information based on commonalities. This technique is known as pattern matching and is often used to query databases.
For example, to return data for employees whose last name starts with AL, you build a query that looks like this:
<cfquery name="GetEmployees" datasource="CompanyInfo">
SELECT FirstName, LastName, StartDate, Salary, Contract FROM Employee WHERE LastName LIKE 'AL%' </cfquery>
To return information from the Departmt table on all departments except the sales department, you would build a query that looks like this:
<cfquery name="GetDepartments" datasource="CompanyInfo">
SELECT * FROM Departmt WHERE Dept_Name NOT LIKE '[Ss]ales' </cfquery>
Note Whether SQL identifiers and data comparison operations are case sensitive depends on the database. |
When you want to retrieve data based on the results of more than one comparison you can use AND and OR operators to combine conditions.
For example, to return data for contract employees who earn more than $50,000, would build a query that looks like this:
<cfquery name="GetEmployees" datasource="CompanyInfo">
SELECT FirstName, LastName, StartDate, Salary, Contract FROM Employee WHERE Contract = 'Yes' AND Salary > 50000 </cfquery>
Many times, the data that you want to retrieve is maintained in multiple tables. For example, in the database that you are working with:
To compare and retrieve data from more than one table during a query, use the WHERE clause to join two tables through common information.
For example, to return employee names, start dates, department names, and salaries for employees who work for the HR department, you build a query that looks like this:
<cfquery name="GetEmployees" datasource="CompanyInfo">
SELECT Departmt.Dept_Name, Employee.FirstName, Employee.LastName, Employee.StartDate, Employee.Salary FROM Departmt, Employee WHERE Departmt.Dept_ID = Employee.Dept_ID AND Departmt.Dept_Name = 'HR' </cfquery>
In this example, the following criteria joins the two tables:
Departmt.Dept_ID = Employee.Dept_ID
It ensures that each row of the query results contains the department name from the Departmt table that corresponds to the Department ID in this employee's row in the Employee table. Without this statement, the query returns a row for every employee in the Employee table, and all rows have the Dept_Name HR, even if the employee is not in the HR department.
When you do table joins, keep the following information in mind:
If you want users to optionally enter multiple search criteria, you can wrap conditional logic around the SQL AND clause to build a flexible search interface. To test for multiple conditions, wrap additional cfif
tags around additional AND clauses.
The following action page allows users to search for employees by department, last name, or both.
a
ctionpage.cfm in ColdFusion Studio.
<html> <head> <title>Retrieving Employee Data Based on Criteia from Form</title> </head> <body> <cfquery name="GetEmployees" datasource="CompanyInfo"> SELECT Departmt.Dept_Name, Employee.FirstName, Employee.LastName, Employee.StartDate, Employee.Salary FROM Departmt, Employee WHERE Departmt.Dept_ID = Employee.Dept_ID <cfif IsDefined("FORM.Department")> AND Departmt.Dept_Name = '#Form.Department#' </cfif> <cfif Form.LastName IS NOT ""> AND Employee.LastName = '#Form.LastName#' </cfif> </cfquery> <h4>Employee Data Based on Criteria from Form</h4> <table> <tr> <th>First Name</th> <th>Last Name</th> <th>Salary</th> </tr> <cfoutput query="GetEmployees"> <tr> <td>#FirstName#</td> <td>#LastName#</td> <td>#Salary#</td> </tr> </cfoutput> </table> </body> </html>
The following table describes the highlighted code and its function: