Displaying Data


<cfoutput> is used to resolve and display dynamic ColdFusion variables. Coupled with a query object such as the result of a <cfquery> and SELECT statement, <cfoutput> can be used to loop over results, once per record in the record set:

 <cfquery name="users"          datasource="dsn"> SELECT LastName, FirstName, Phone FROM Users </cfquery> <table>   <tr bgcolor="#cccccc">     <th>Name</th>     <th>Phone</th>   </tr>   <cfoutput query="users">     <tr bgcolor="#IIF(users.CurrentRow MOD 2,                      de("##ededed"),                      de("##ffffff"))#">       <td>#LastName#, #Firstname#</td>       <td>#phone#</td>     </tr>   </cfoutput> </table> 

<cfoutput> loops over the block of code contained within the opening and closing tags once per record in the record set of the query nominated in the query attribute. The column name in the query is prefixed with the name of the query itself to define the variable. The value of the variable changes with each iteration to reflect the value of the column for the current row in the record set. The dynamic IIf() function uses the users.CurrentRow variable of the query to alternate the background color of the table rows, and the users.CurrentRow variable changes with every iteration to reflect the current record number.

<cfoutput> is also covered in Chapter 2, "Working with Variables and Expressions."


IIf() and DE() are covered in Chapter 22, "Dynamic Functions."


NOTE

Query variables don't always need the query name prefix. If the variable is referenced inside <cfoutput> tags with the same query object specified in the query attribute, you do not need the query name as a prefix. The <cfoutput> query variables are the first to be resolved, so there is no chance of a variable name conflict.


Referencing a query variable from within <cfoutput> tags without a query attribute specified, or indeed with a different query name nominated, is still possible:

 <cfoutput>#users.LastName#</cfoutput> <cfoutput query="users">#departments.DeptName#</cfoutput> 

The variable resolves the value of the first record in the query only.

NOTE

To reference a specific record in the record set of a query object, you can use the row number in the same way as an array position. For example, to reference the LastName column of the second row of the query users with array syntax, use this:

 #users.LastName[2]# 




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net