ColdFusion displays query results

Source: Internet
Author: User

Display query results

Once you create a query in the application file, you can reference the query results in other cfm files. Query results can be used to dynamically generate HTML webpages.

When you learn how to use CFML labels, remember that you can also use HTML labels and text in programs. ColdFusion simply transmits tags and text directly to the client browser where you use HTML tags and text. The most flexible way to output query results is to use the cfoutput tag. The cfoutput tag can be used to output a specific query or multiple queries. A cfoutput tag can contain:

· Plain text

· HTML Tag

· Reference query Fields

· Reference dynamic parameters, such as form fields

· Functions

The basic output Code has the following syntax:

<Cfoutput query = "queryname" maxrows = n>

Literal text, HTML tags, and

Dynamic Field references (e.g., # fullname #)

</Cfoutput>

Database field name in cfoutput

Some databases, such as Microsoft Access, can contain spaces in field names, such as "region name .". ColdFusion does not support reference such fields in cfoutput. To be compatible with ColdFusion, avoid using spaces in table names and field names, which gives applications maximum compatibility with different database systems. You can use underlines as separators, such as "region_name ".

The field name must start with a letter and can only contain letters and numbers. You can use the SQL keyword as to create an alias for the field name. For example, the statement:

Select 401 K as FK from employee_withholding

The cfquery can be correctly processed.

Example: cfoutput

In the following example, we use the cfoutput tag to display the query results of the employeelist query. Select the name, name, and phone number of each employee. Specifying a query name in a variable name is a good programming habit.

The complete code of the program is given below:

<! --- Query to select MERs --->

<Cfquery name = "employeelist" datasource = "companydb">

Select *

From employees

</Cfquery>

<HTML>

<Head>

<Title> employee list </title>

</Head>

<Body>

<H2> employee list </H2>

<! --- Output section --->

<Cfoutput query = "employeelist">

<HR>

# Employeelist. firstname # employeelist. lastname #

(PHONE: # employeelist. phonenumber #) <br>

</Cfoutput>

</Body>

</Html>

You can directly apply for this webpage through URL, for example:

Http: // myserver/cfdocs/employeelist. cfm

Or use a hyperlink to reference:

<A href = "myserver/cfdocs/employeelist. cfm"> employee list </a>

The HTML code of the output result in this example is similar:

<HR>

Deborah Jones (phone: 612-227-1019) <br>

<HR>

John smith (phone: 507-452-7224) <br>

<HR>

Frank Wilson (phone: 612-831-9555) <br>

Nested cfoutput and group

You can nest cfoutput to display query results in groups. The implementation of grouping is to specify the group attribute of the cfoutput tag, and then embed another cfoutput into the previous cfoutput. The content above and below in the inner cfoutput is used to display a title and footer information for each group.

Example: Group

The following code uses the group attribute of cfoutput to display the query result group according to the value of "courselevel. The order by keyword sorts the query results based on the value of the courselevel field. Cfoutput nesting does not limit the number of layers. If you want to use multi-level grouping, you need to set multi-level sorting in SQL queries (for example, "order by region, state ").

<Cfquery name = "courses" datasource = "coursedb">

Select *

From courselist

Where department_id =? Form. Department #

Order by courselevel

</Cfquery>

<Cfoutput query = "courses" group = "courselevel">

<H4> # courselevel # </H4>

<Ul>

<Cfoutput>

<Li> # coursenumber #-# coursename #

</Cfoutput>

</Ul>

</Cfoutput>

In this example, the result displayed in the browser is:

Basic

100-physiology

Intermediate

510-neurobiology

500-Plant Biology

Advanced

820-neurobiology

800-Microbiology

Cfquery attributes

Each cfquery has three attributes that provide the number of records.

Number of records

Number of records

Description

Recordcount

Query the total number of returned records.

Currentrow

The current record being processed by cfoutput.

Columnlist

Returns the list of query fields separated by commas.

Example: query a field

In this example, the number of records in the customerlist query and the list of query fields are returned:

<Cfoutput>

The search returned information

On # customerlist. recordcount # customers. <br>

Columns queried were # customerlist. columnlist #.

</Cfoutput>

To display the sequence number of each record, you can use the following syntax:

<Cfoutput query = "customerlist">

# Currentrow #-# firstname # lastname # <br>

</Cfoutput>

Returns part of a record set.

For a large record set, you may want to display only a portion of the record set. This can be done using the startrow and maxrows attributes of the cfoutput tag. Startrow specifies the first record in the record set to be returned. Maxrows specifies the total number of records to be returned.

Example: A part of a record set

In this example, 10th to 20 records are queried in the result record set of employeelist:

<Cfoutput query = "employeelist"

Startrow = "10" maxrows = "20">

# Firstname # lastname # Phone # <br>

</Cfoutput>

Use parameters in cfoutput

Cfoutput is not only used to display the queried data. You can also use cfoutput to display form variables, URL variables, Cookie, client variables, server variables, session variables, Application variables, and CGI environment variables. Note the correct prefix When referencing these variables so that ColdFusion can distinguish these variables from the field names of the query results. Like the query field, the variable must be added to the well number. The parameter values of each record in the query results are displayed once. Note: To display the well number itself, apply two consecutive well numbers to indicate one well number.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.