Paging ColdFusion Results

by @jehiah on 2005-01-14 16:33
Filed under: All, ColdFusion

Everyone has to page through a set of query results sooner or later. This is the easiest block of code I have to do it.

Example
Example of Recordset Paging

<cfparam name="start" type="numeric" default="1">
<cfparam name="step" type="numeric" default="10">
<cfquery datasource="..." cachedwithin=".01" name="queryResults">
select * from table
</cfquery>
<cfif queryResults.recordcount gt 0>
<cfoutput>
    <p>
    <!--- if past start --->
    <cfif (start-step-step) gt 1>
        <a href="#cgi.SCRIPT_NAME#?start=1"><img src="/images/beginning_blue.png" alt="Beginning" align="absbottom"></a>
    </cfif> 
    <cfif start gt 1>
        <a href="#cgi.SCRIPT_NAME#?start=#start-step#"><img src="/images/previous_blue.png" alt="Previous" align="absbottom"></a>
    </cfif>
    <strong>#start# - #iif(start + step gt queryResults.recordcount,queryResults.recordcount,start + step-1)# of #queryResults.recordcount# records</strong>
    <!--- if still some not displayed --->
    <cfif (start + step) lte queryResults.recordcount>
        <a href="#cgi.SCRIPT_NAME#?start=#start+step#"><img src="/images/next_blue.png" alt="Next" align="absbottom"></a>
    </cfif>
    <cfif (start+step+step) lte queryResults.recordcount>
        <a href="#cgi.SCRIPT_NAME#?start=#queryResults.recordcount-step+1#"><img src="/images/end_blue.png" alt="End" align="absbottom"></a>
    </cfif> 
    </p>    
</cfoutput>
</cfif>
<cfloop query="queryResults" startrow="#start#" endrow="#start + step-1#"> 
...... output query here
</cfloop>

Yes, for those that want to know, you can grab those beautifull blue images from my site.

Subscribe via RSS ı Email
© 2017 - Jehiah Czebotar