View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfoutput PreviousNext

cfoutput

Displays output that can contain the results of processing ColdFusion variables and functions. Can loop over the results of a database query.

Data output tags

<cfoutput 
query = "query_name"
group = "query_column"
groupCaseSensitive = "Yes" or "No"
startRow = "start_row"
maxRows = "max_rows_output">
</cfoutput>

cfcol, cfcontent, cfdirectory, cftable

Attribute

Req/Opt

Default

Description

query

Optional

 

Name of cfquery from which to draw data for output section.

group

Optional

 

Query column to use to group sets of records. Eliminates adjacent duplicate rows when data is sorted. Use if you retrieved a record set ordered on one or more a query columns. For example, if a record set is ordered on "Customer_ID" in the cfquery tag, you can group the output on "Customer_ID."

groupCaseSensitive

Optional

Yes

Boolean. Whether to consider the case in grouping rows.

startRow

Optional

1

Row from which to start output.

maxRows

Optional

displays all rows

Maximum number of rows to display.

In the cfoutput tag body, ColdFusion treats text that is surrounded by pound signs (#) as a ColdFusion variable or function call. For example, the following code displays the text "Hello World!":

<cfset myVar="Hello World!">
cfoutput>#myVar#</cfoutput>

When you specify a query attribute, this tag loops over the query rows and produces output for each row within the range specified by the startRow and maxRows values, and groups or eliminates duplicate entries as specified by the grouping attribute values, if any. It also sets the query.currentRow variable to the current row being processed.

If you nest cfoutput blocks that process a query, you specify the query and group attributes at the top-most level; you can specify a group attribute for each inner block except the innermost cfoutput block.

This tag requires an end tag.

<!--- This example shows how cfoutput operates --->
<!--- run a sample query --->
<cfquery name = "GetCourses" dataSource = "cfsnippets">
   SELECT Dept_ID, CorName, CorLevel
   FROM courseList
   ORDER by Dept_ID, CorLevel, CorName
</cfquery>
<h3>cfoutput Example</h3>
<p>cfoutput tells ColdFusion Server to begin processing, and then 
to hand back control of page rendering to the web server. <p>For example, to show today's date, you could write #DateFormat("#Now()#").
If you enclosed that expression in cfoutput, the result would be
<cfoutput>#DateFormat(Now())#</cfoutput>. <p>In addition, cfoutput may be used to show the results of a query
operation, or only a partial result, as shown: <p>There are <cfoutput>#getCourses.recordCount#</cfoutput> total records
in our query. Using the maxRows parameter, we are limiting our
display to 4 rows. <p><cfoutput query = "GetCourses" maxRows = 4> #Dept_ID# #CorName# #CorLevel#<br> </cfoutput> <p>The next example uses the group attribute to eliminate duplicate lines
from a list of course levels taugh in each department.</p> <p><cfquery name = "GetCourses" dataSource = "cfsnippets">
SELECT Dept_ID, CorLevel
FROM courseList
ORDER by Dept_ID, CorLevel </cfquery> <p><cfoutput query = "GetCourses" group="CorLevel" GroupCaseSensitive="True">
#Dept_ID# #CorLevel#<br> </cfoutput> <p>cfoutput can also show the results of a more complex expression, such as getting the day of the week from today's date. We first extract the integer representing the Day of the Week from the server function Now() and then apply the result to the DayofWeekAsString function: <br>Today is #DayofWeekAsString(DayofWeek(Now()))# <br>Today is <cfoutput>#DayofWeekAsString(DayofWeek(Now()))#</cfoutput>

Contents > CFML Reference > ColdFusion Tags > cfoutput PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


wlee said on Mar 26, 2004 at 10:38 AM :
We are working to improve the examples in the ColdFusion reference pages. We propose to replace the current example on this page with the the following example. If you have any comments on this example, add them to this page.

<!--- This example shows how cfoutput operates --->
<!--- run a sample query --->
<cfquery name = "GetCourses" dataSource = "cfsnippets">
SELECT Dept_ID, CorName, CorLevel
FROM courseList
ORDER by Dept_ID, CorLevel, CorName
</cfquery>
<h3>cfoutput Example</h3>
<p>cfoutput tells ColdFusion Server to begin processing, and then to hand back control of page rendering to the web server.
<p>For example, to show today's date, you could write #DateFormat("#Now()#"). If you enclosed that expression in cfoutput, the result would be<cfoutput>#DateFormat(Now())#</cfoutput>.

<p>In addition, cfoutput may be used to show the results of a query operation, or only a partial result, as shown:

<p>There are <cfoutput>#getCourses.recordCount#</cfoutput> total records in our query. Using the maxRows parameter, we are limiting our display to 4 rows.
<p><cfoutput query = "GetCourses" maxRows = 4>
#Dept_ID# #CorName# #CorLevel#<br>
</cfoutput>

<p>The next example uses the group attribute to eliminate duplicate lines from a list of course levels taught in each department.</p>
<p><cfquery name = "GetCourses" dataSource = "cfsnippets">SELECT Dept_ID, CorLevel FROM courseList ORDER by Dept_ID, CorLevel
</cfquery>
<p><cfoutput query = "GetCourses" group="CorLevel" GroupCaseSensitive="True">#Dept_ID# #CorLevel#<br>
</cfoutput>

<p>cfoutput can also show the results of a more complex expression,
such as getting the day of the week from today's date. We first
extract the integer representing the Day of the Week from
the server function Now() and then apply the result to
the DayofWeekAsString function:

<br>Today is #DayofWeekAsString(DayofWeek(Now()))#
<br>Today is <cfoutput>#DayofWeekAsString(DayofWeek(Now()))#</cfoutput>
EscapedCharacter said on Jul 28, 2004 at 11:16 AM :
I suggest that you consider expanding the example to demonstrate nested CFOUTPUT tags.
No screen name said on Sep 22, 2004 at 4:37 AM :
I agree that the example for groups MUST have nested cfoutput tags to
display the information that is nested within the group sets.
jrunrandy said on Sep 22, 2004 at 5:09 AM :
Here is a nested cfoutput example:
<cfquery datasource="CompanyInfo" name="empSalary">
SELECT Emp_ID, firstname, lastname, e.dept_id, salary, d.dept_name
FROM employee e, departmt d
WHERE e.dept_id = d.dept_id
ORDER BY d.dept_name
</cfquery>

<!--- outer cfoutput --->
<cfoutput query="empSalary" group="dept_id">
<h2>#dept_name#</h2>
<table width="95%" border="2" cellspacing="2" cellpadding="2" >
<tr>
<th>Employee</th>
<th>Salary</th>
</tr>
<cfset deptTotal = 0 >
<!--- inner cfoutput --->
<cfoutput>
<tr>
<td>#empSalary.lastname#, #empSalary.firstname#</td>
<td align="right">#DollarFormat(empSalary.salary)#</td>
</tr>
<cfset deptTotal = deptTotal + empSalary.salary>
</cfoutput>
<tr>
<td align="right">Total</td>
<td align="right">#DollarFormat(deptTotal)#</td>
</tr>
<cfset deptTotal = 0>
</table>
</cfoutput>
No screen name said on Nov 15, 2004 at 2:24 AM :
Nested cfoutput blocks:
can you nest cfoutput blocks with different queries
for instance
outer query: pending order
inner query: articles ordered for this order
or do you have to join the data in a single query and use grouping?
J4MIE said on Dec 1, 2004 at 6:53 PM :
You can't nest <cfoutput> tags but within one you can do a loop through the query:

<cfloop query="myQuery">
#var#
</cfloop>
tim8w said on Feb 4, 2005 at 11:22 AM :
I am hoping that you can answer my question. I am using cfmail to send form data to an e-mail. The proces works great. The problem is in the format of the data. In my form, I have a textarea with the WRAP set to "hard". When cfmail e-mails the textarea, the text is formatted correctly, but when I use cfoutput to display what was sent, the field is displayed all on one line. Any ideas?

Thanks,
Tim
cubiclesrule said on Jun 28, 2007 at 8:37 AM :
J4MIE, you can nest CFOUTPUT if you use the GROUP attribute:

<cfset bShowEachLine = false>
<cfquery name="q1" datasource="#database#">
select * from employees
</cfquery>
<cfoutput query="q1" group="company_code">
<li>#company_code#: #currentrow#, #recordcount#</li>
<cfif bShowEachLine is true>
<ol>#first_name# #last_name#: #currentrow#, #recordcount#</ol>
<cfelse>
<cfoutput><ol>#first_name# #last_name#: #currentrow#, #recordcount#</ol></cfoutput>
</cfif>
</cfoutput>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-b12.htm