• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. Database Basics > Looping Over a Query Result Set

4.7. Looping Over a Query Result Set

As I mentioned briefly in Chapter 2, a query loop (CFLOOP tag with the QUERY attribute) performs essentially the same job as using a CFOUTPUT tag with the QUERY attribute. A query loop iterates over each row in a query object. Optionally, a start row and end row within the query may be specified:

<CFLOOP QUERY="query_name"
        STARTROW="row_number"
        ENDROW="row_number">
...
</CFLOOP>

The QUERY attribute specifies the name of a valid ColdFusion query object. STARTROW is optional and may be used to specify the row within the query object where the loop should begin. ENDROW is also optional and specifies the last row within a query object that should be included within the loop.

The query loop may be used instead of the QUERY attribute of the CFOUTPUT tag to display the contents of a query:

<CFQUERY NAME="GetEmployeeInfo" DATASOURCE="ProgrammingCF">
         SELECT Name, Title
         FROM EmployeeDirectory
</CFQUERY>

<CFLOOP QUERY="GetEmployeeInfo">
  <CFOUTPUT>#Name#, #Title#<BR></CFOUTPUT>
</CFLOOP>

Using a query loop allows you to work around limitations inherent in the CFOUTPUT tag such as the inability to nest additional output queries within a CFOUTPUT block. For example, the following code produces an error in ColdFusion because you can't nest CFOUTPUT tags without using the GROUP attribute:

<CFQUERY NAME="MyQuery1" DATASOURCE="MyDSN">
    SELECT *
    FROM MyTable
    WHERE Field = Value
</CFQUERY>

<CFOUTPUT QUERY="MyQuery1">
  <CFQUERY NAME="MyQuery2" DATASOURCE="MyDSN">
      SELECT *
      FROM MyTable
      WHERE Field = Value
  </CFQUERY>

  <CFOUTPUT QUERY="MyQuery2">
    Additional processing and output code here...
  </CFOUTPUT>
</CFOUTPUT>

You can get around this limitation by using a query loop within the CFOUTPUT block:

<CFQUERY NAME="MyQuery1" DATASOURCE="MyDSN">
    SELECT *
    FROM MyTable
    WHERE Field = Value
</CFQUERY>

<CFOUTPUT QUERY="MyQuery1">
  <CFQUERY NAME="MyQuery2" DATASOURCE="MyDSN">
      SELECT *
      FROM MyTable
      WHERE Field = Value
  </CFQUERY>

  <CFLOOP QUERY="MyQuery2">
    Additional processing and output code here...
  </CFLOOP>
</CFOUTPUT>

Additionally, you can use the query loop to output a section of a record set by dynamically defining the start row and end row of the query object to loop over:

<CFSET TheStart = 3>
<CFSET TheEnd = 5>

<CFQUERY NAME="GetEmployeeInfo" DATASOURCE="ProgrammingCF">
         SELECT Name, Title
         FROM EmployeeDirectory
</CFQUERY>

<CFLOOP QUERY="GetEmployeeInfo"
        STARTROW="#TheStart#"
        ENDROW="#TheEnd#">
  <CFOUTPUT>#Name#, #Title#<BR></CFOUTPUT>
</CFLOOP>

This technique can be used to create a next/previous record browser in which a predetermined number of rows from a query result set are displayed on the page. This allows users to browse a set number of records at a time while moving forward and backward through the record set. Next/previous browsing is discussed in detail in Chapter 11.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint