coldfusion Query


Example

Consider the table dbo.state_zip, which contains the columns city, statecode and zipcode and has over 80,000 records.

Parameters

AttributeRequiredTypeDefaultDescription
querytruestringThe variable name of a query object.
startrowfalsenumericThe starting row index of the query object.
endrowfalsenumericThe ending row index of the query object.
groupfalsestringThe query column name on which to group records.

Example query

<cfquery name="geo" datasource="reotrans-dev">
    SELECT city, stateCode, zipCode
    FROM dbo.state_zip
</cfquery>

Tag syntax

Using the query object geo as the source for cfloop. Since the table dbo.state_zip has so many records, the HTML generated will take quite some time. This example shows only the first 20 records' worth of HTML.

<cfoutput>
    <ul>
    <cfloop query="geo">
        <!--- Scope the column names with the query name. --->
        <li>#geo.city# | #geo.stateCode# | #geo.zipCode#</li>
    </cfloop>
    </ul>
</cfoutput>

Generated HTML

<ul>
    <li>100 PALMS | CA | 92274</li>
    <li>1000 PALMS | CA | 92276</li>
    <li>12 MILE | IN | 46988</li>
    <li>1ST NATIONAL BANK OF OMAHA | NE | 68197</li>
    <li>29 PALMS | CA | 92277</li>
    <li>29 PALMS | CA | 92278</li>
    <li>3 STATE FARM PLAZA | IL | 61710</li>
    <li>3 STATE FARM PLAZA | IL | 61791</li>
    <li>30TH STREET | PA | 19104</li>
    <li>3M CORP | MN | 55144</li>
    <li>65TH INFANTRY | PR | 00923</li>
    <li>65TH INFANTRY | PR | 00924</li>
    <li>65TH INFANTRY | PR | 00929</li>
    <li>65TH INFANTRY | PR | 00936</li>
    <li>7 CORNERS | VA | 22044</li>
    <li>88 | KY | 42130</li>
    <li>9 MILE POINT | LA | 70094</li>
    <li>A A R P INS | PA | 19187</li>    
    <li>A A R P PHARMACY | CT | 06167</li>
    <li>A H MCCOY FEDERAL BLDG | MS | 39269</li>
</ul>

Limiting output to specific rows

To limit the query's output to a specific range of rows, specify startrow and endrow.

<cfloop query="geo" startrow="100" endrow="150">
    <li>#geo.city# | #geo.stateCode# | #geo.zipCode#</li>
</cfloop>

Grouping Output

In the example data, the same state listed multiple times in relation to the multiple cities that are associated to each state. You can also see the same city listed multiple times in relation to the multiple zip codes associated to each city.

Let's group the output by state first. Notice the 2nd instance of cfloop wrapped around the content that will be output under the stateCode grouped content.

<cfoutput>
    <ul>
    <cfloop query="geo" group="stateCode">
        <!--- Scope the column names with the query name. --->
        <li>#geo.stateCode#
            <ul>
                <cfloop>
                    <li>#geo.city# | #geo.zipCode#</li>
                </cfloop>
            </ul>
        </li>
    </cfloop>
    </ul>
</cfoutput>

Generated HTML (extract) from one grouped cfloop tag.

<ul>
    <li>AK
        <ul>
            <li>KONGIGANAK | 99545</li>
            <li>ADAK | 99546</li>
            <li>ATKA | 99547</li>
            <!-- etc. -->
        </ul>
    </li>
    <li>AL
        <ul>
            <li>ALEX CITY | 35010</li>
            <li>ALEXANDER CITY | 35010</li>
            <li>ALEX CITY | 35011</li>
            <!-- etc. -->
        </ul>
    </li>
    <!-- etc. -->
</ul>

Finally, let's group the output by stateCode, then by city in order to see all the zipCode entries per city. Notice the 2nd cfloop is now grouped by city and a 3rd cfloop exists to output the zipCode data.

<cfoutput>
    <ul>
    <cfloop query="geo" group="stateCode">
        <li>#geo.stateCode#
            <ul>
            <cfloop group="city">
                <li>#geo.city#
                    <ul>
                        <cfloop>
                            <li>#geo.zipCode#</li>
                        </cfloop>
                    </ul>
                </li>
            </cfloop>
            </ul>
        </li>
    </cfloop>
    </ul>
</cfoutput>

Generated HTML (extract) from two grouped cfloop tags.

<ul>
    <li>AK
        <ul>
            <li>ADAK
                <ul>
                    <li>99546</li>
                    <li>99571</li>
                </ul>
            </li>
            <li>AKHIOK
                <ul>
                    <li>99615</li>
                </ul>
            </li>
            <!--- etc. --->
            <li>BARROW
                <ul>
                    <li>99723</li>
                    <li>99759</li>
                    <li>99789</li>
                    <li>99791</li>
                </ul>
            </li>
            <!--- etc. --->
        </ul>
    </li>
    <!--- stateCodes etc. --->
</ul>

CFScript

ColdFusion 6 (MX) though current

<cfscript>
    for (x = 1; x LTE geo.recordcount; x = x + 1) {
        writeOutput( '<li>' & geo.city[x] & ' | ' & 
            geo.stateCode[x] & ' | ' & geo.zipCode[x] & '</li>');
    }
</cfscript>

ColdFusion 8 though current

<cfscript>
    for (x = 1; x <= geo.recordcount; x++) {
        writeOutput( '<li>' & geo.city[x] & ' | ' & 
            geo.stateCode[x] & ' | ' & geo.zipCode[x] & '</li>');
    }
</cfscript>

ColdFusion 10 though current

With the FOR IN syntax, x is a query row object, not the row index.

<cfscript>
    for (x in geo) {
        writeOutput( '<li>' & x.city & ' | ' & 
            x.stateCode & ' | ' & x.zipCode & '</li>');
    }
</cfscript>

ColdFusion 11 though current

ColdFusion 11 allows most tags to be written as cfscript.

<cfscript>
    cfloop(query: geo, startrow: 1, endrow: 2) {
        writeOutput( '<li>' & geo.city & ' | ' & 
            geo.stateCode & ' | ' & geo.zipCode & '</li>');
    }
</cfscript>

With group.

<cfscript>
    cfloop(query: geo, group: 'city') {
        writeOutput( '<li>' & geo.city & '<ul>');
        cfloop() { // no arguments, just as in the tag syntax.
            writeOutput('<li>'  & geo.zipCode & '</li>');
        }
        writeOutput('</ul></li>');
    }
</cfscript>