When you return your results to the user, you must make sure that your pages respond to the user's needs and are appropriate for the type and amount of information. In particular you must consider the following situations:
Your code must accommodate the cases where a query does not return any records. To determine whether a search has retrieved records, use the RecordCount
query variable. You can use the variable in a conditional logic expression that determines how to display search results appropriately to users.
For more information on query variables, including RecordCount
, see Chapter 20, "Accessing and Retrieving Data".
For example, to inform the user when no records were found by the GetEmployees query, insert the following code before displaying the data:
<cfif GetEmployees.RecordCount IS "0">
No records match your search criteria. <BR> <cfelse>
RecordCount
with the query name.
cfif
tag that displays a message to the user.cfelse
tag to format the returned data.</cfif>
tag end to indicate the end of the conditional code.<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 = <cfqueryparam value="#Form.Department#" CFSQLType="CF_SQL_VARCHAR"> </cfif> <cfif Form.LastName is not ""> AND Employee.LastName = <cfqueryparam value="#Form.LastName#" CFSQLType="CF_SQL_VARCHAR"> </cfif> </cfquery> <cfif GetEmployees.recordcount is "0"> No records match your search criteria. <br> Please go back to the form and try again. <cfelse> <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> </cfif> </table> </body> </html>
You can use the cfflush
tag to incrementally output long-running requests to the browser before a ColdFusion page is fully processed. This allows you to give the user quick feedback when it takes a long time to complete processing a request. For example, when a request takes time to return results, you can use cfflush
to display the message, "Processing your request -- please wait.". You can also use it to incrementally display a long list as it gets retrieved.
The first time you use the cfflush
tag on a page, it sends to the browser all of the HTML headers and any other available HTML. Subsequent cfflush
tags on the page send only the output that ColdFusion generates since the previous flush.
You can specify an interval
attribute to tell ColdFusion to flush the output each time that at least the specified number of bytes become available. (The count does not include HTML headers and any data that is already available when you make this call.) You can use the cfflush
tag in a cfloop
to incrementally flush data as it becomes available. This format is particularly useful when a query responds slowly with large amounts of data.
When you flush data, make sure that a sufficient amount of information is available, because some browsers might not respond if you flush only a very small amount. Similarly, if you use an interval
attribute, set it for a reasonable size, such as a few hundred bytes or more but not many thousands of bytes.
Caution: After you use the cfflush
tag on a page, any CFML function or tag on the page that modifies the HTML header causes an error. These include the cfcontent, cfcookie,
cfform,
cfheader,
cfhtmlhead,
and cflocation
tags. You also get an error if you use the cfset
tag to set a Cookie scope variable. You can catch all errors, except Cookie errors, with a cfcatch type="template"
tag. Catch cookie errors with cfcatch type="Any"
.
The following example uses the cfloop
tag and the rand()
random number generating function to artificially delay the generation of data for display. It simulates a situation in which it takes time to retrieve the first data and additional information becomes available slowly.
<html>
<head> <title>Your Magic numbers</title> </head> <body> <h1>Your Magic numbers</h1> <P>It will take us a little while to calculate your ten magic numbers. It takes a lot of work to find numbers that truly fit your personality. So relax for a minute or so while we do the hard work for you.</P> <h2>We are sure you will agree it was worth the short wait!</h2> <cfflush> <cfflush interval=10> <!--- Delay Loop to make is seem harder ---> <cfloop index="randomindex" from="1" to="200000" step="1"> <cfset random=rand()> </cfloop> <!--- Now slowly output 10 random numbers ---> <cfloop index="Myindex" from="1" to="10" step="1"> <cfloop index="randomindex" from="1" to="100000" step="1"> <cfset random=rand()> </cfloop> <cfoutput> Magic number number #Myindex# is: #RandRange( 100000, 999999)#<br><br> </cfoutput> </cfloop> </body> </html>
The following table describes the code and its function: