For the complete experience, please enable JavaScript in your browser. Thank you!

  • Creative Cloud
  • Photoshop
  • Illustrator
  • InDesign
  • Premiere Pro
  • After Effects
  • Lightroom
  • See all
  • See plans for: businesses photographers students
  • Document Cloud
  • Acrobat DC
  • eSign
  • Stock
  • Elements
  • Marketing Cloud
  • Analytics
  • Audience Manager
  • Campaign
  • Experience Manager
  • Media Optimizer
  • Target
  • See all
  • Acrobat Reader DC
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player
  • All products
  • Creative Cloud
  • Individuals
  • Photographers
  • Students and Teachers
  • Business
  • Schools and Universities
  • Marketing Cloud
  • Document Cloud
  • Stock
  • Elements
  • All products
  • Get Support
    Find answers quickly. Contact us if you need to.
    Start now >
  • Learn the apps
    Get started or learn new ways to work.
    Learn now >
  • Ask the community
    Post questions and get answers from experts.
    Start now >
    • About Us
    • Careers At Adobe
    • Investor Relations
    • Privacy  |  Security
    • Corporate Responsibility
    • Customer Showcase
    • Events
    • Contact Us
News
    • 3/22/2016
      Adobe Summit 2016: Are You An Experience Business?
    • 3/22/2016
      Adobe Announces Cross-Device Co-op to Enable People-Based Marketing
    • 3/22/2016
      Adobe and comScore Advance Digital TV and Ad Measurement
    • 3/22/2016
      Adobe Marketing Cloud Redefines TV Experience
Introduction to Retrieving and Formatting Data / 

Working with queries and data

Adobe Community Help


Applies to

  • ColdFusion

Contact support

 
By clicking Submit, you accept the Adobe Terms of Use.
 

The ability to generate and display query data is one of the most important and flexible features of ColdFusion. Some of these tools are effective for presenting any data, not just query results.

Using HTML tables to display query results

You can use HTML tables to specify how the results of a query appear on a page. To do so, you place the cfoutput tag inside the table tags. You can also use the HTML th tag to place column labels in a header row. To create a row in the table for each row in the query results, place the tr block inside the cfoutput tag. 
In addition, you can use CFML functions to format individual pieces of data, such as dates and numeric values.

Place the query results in a table
  1. Open the ColdFusion actionpage.cfm page in your editor.
  2. Modify the page so that it appears as follows:

    <html>
    <head>
    <title>Retrieving Employee Data Based on Criteria from Form</title>
    </head>

    <body>
    <cfquery name="GetEmployees" datasource="cfdocexamples">
    SELECT FirstName, LastName, Salary
    FROM Employee
    WHERE LastName=<cfqueryparam value="#Form.LastName#"
    CFSQLType="CF_SQL_VARCHAR">
    </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>
    <cfif IsDefined("Form.Contractor")>
    <cfoutput>Contractor: #Form.Contractor#</cfoutput>
    </cfif>
    </body>
    </html>

  3. Save the page as actionpage.cfm in the myapps directory.
  4. View the formpage.cfm page in your browser.
  5. Enter Smith in the Last Name text box and submit the form.The records that match the criteria specified in the form appear in a table.
Reviewing the code

The following table describes the highlighted code and its function:

Code

Description

 

<table>

 

Places data into a table.

 

<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Salary</th>
</tr>

 

In the first row of the table, includes three columns, with the headings: First Name, Last Name, and Salary.

 

<cfoutput query="GetEmployees">

 

Tells ColdFusion to display the results of the GetEmployees query.

 

<tr>
<td>#FirstName#</td>
<td>#LastName#</td>
<td>#Salary#</td>
</tr>

 

For each record in the query, creates a row in the table, with three columns that display the values of the FirstName, LastName, and Salary fields of the record.

 

</cfoutput>

 

Ends the output region.

 

</table>

 

Ends the table.

Formatting individual data items

You can format individual data items. For example, you can format the salary data as monetary values. To format the salary data using the dollar format, you use the CFML function DollarFormat.

Change the format of the Salary
  1. Open the file actionpage.cfm in your editor.
  2. Change the following line:

    <td>#Salary#</td>

    to

    <td>#DollarFormat(Salary)#</td>

  3. Save the page.

Building flexible search interfaces

One option with forms is to build a search based on the form data. For example, you could use form data as part of the WHERE clause to construct a database query.
To give users the option to enter multiple search criteria in a form, you can wrap conditional logic around a SQL AND clause as part of the WHERE clause. The following action page allows users to search for employees by department, last name, or both.

Build a more flexible search interface
  1. Open the ColdFusion actionpage.cfm page in your editor.
  2. Modify the page so that it appears as follows:

    <html>
    <head>
    <title>Retrieving Employee Data Based on Criteria from Form</title>
    </head>
    <body>
    <cfquery name="GetEmployees" datasource="cfdocexamples">
    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>

    <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>

  3. Save the file.
  4. View the formpage.cfm page in your browser.
  5. Select a department, optionally enter a last name, and submit the form.
Reviewing the code

The following table describes the highlighted code and its function:

Code

Description

 

SELECT Departmt.Dept_Name,
Employee.FirstName,
Employee.LastName,
Employee.StartDate,
Employee.Salary
FROM Departmt, Employee
WHERE Departmt.Dept_ID = Employee.Dept_ID

 

Retrieves the fields listed from the Departmt and Employee tables, joining the tables based on the Dept_ID field in each table.

 

<cfif IsDefined("Form.Department")>
AND Departmt.Dept_Name=<cfqueryparam value="#Form.Department#"
CFSQLType="CF_SQL_VARCHAR">
</cfif>

 

If the user specified a department on the form, only retrieves records where the department name is the same as the one that the user specified. Use number signs (#) in the SQL AND statement to identify Form.Department as a ColdFusion variable, but not in the IsDefined function.

 

<cfif Form.LastName IS NOT "">
AND Employee.LastName=<cfqueryparam value="#Form.LastName#"
CFSQLType="CF_SQL_VARCHAR">
</cfif>

 

If the user specified a last name in the form, only retrieves the records in which the last name is the same as the one that the user entered in the form.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy

Choose your region United States (Change)   Products   Downloads   Learn & Support   Company
Choose your region Close

Americas

Europe, Middle East and Africa

Asia Pacific

  • Brasil
  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Cyprus - English
  • Česká republika
  • Danmark
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Greece - English
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Malta - English
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • Southeast Asia (Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam) - English
  • 台灣

Commonwealth of Independent States

  • Includes Armenia, Azerbaijan, Belarus, Georgia, Moldova, Kazakhstan, Kyrgyzstan, Tajikistan, Turkmenistan, Ukraine, Uzbekistan

Copyright © 2016 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

AdChoices