Looping over a database query in ColdFusion using the CFLOOP tag

Readability

Looping over a database query in ColdFusion using the CFLOOP tag

In a pre­vi­ous exam­ple, “Query­ing a data­base in Cold­Fu­sion using the CFQUERY tag”, we saw how you could query a data­base in Cold­Fu­sion 9 using the <CFQUERY> tag.

The fol­low­ing exam­ple shows how you can loop over a query object in Cold­Fu­sion using the <CFLOOP> tag.


    SELECT *
    FROM ART



    name: #getArt.ARTNAME#
isSold: #getArt.ISSOLD#
price: #getArt.PRICE#

The pre­ced­ing code pro­duces the fol­low­ing output:

name: charles1
isSold: 1
price: 10000

name: Michael
isSold: 0
price: 13900

name: Freddy
isSold: 1
price: 12500

name: Paulo
isSold: 1
price: 11100

name: Mary
isSold: 1
price: 13550

In a previous example, “Querying a database in ColdFusion using the CFQUERY tag”, we saw how you could query a database in ColdFusion 9 using the <CFQUERY> tag.

The following example shows how you can loop over a query object in ColdFusion using the <CFLOOP> tag.

<cfquery name="getArt" datasource="cfartgallery">
    SELECT *
    FROM ART
</cfquery>
 
<cfloop query="getArt" endRow="5">
    <cfoutput><em>name:</em> #getArt.ARTNAME#<br/>
        <em>isSold:</em> #getArt.ISSOLD#<br/>
        <em>price:</em> #getArt.PRICE#<br/><br/></cfoutput>
</cfloop>

The preceding code produces the following output:

name: charles1
isSold: 1
price: 10000

name: Michael
isSold: 0
price: 13900

name: Freddy
isSold: 1
price: 12500

name: Paulo
isSold: 1
price: 11100

name: Mary
isSold: 1
price: 13550

Leave a Reply

Your email address will not be published.