This tutorial will demonstrate how to incorporate a navigation system the displays next X records per page. Not sure what I mean? Take a look at the same below:

This example will demonstrate how to incorporate it into your query results. The first thing we must do is define two variables:

The first variable we're defining by default is "Start". We're giving this a value of 1. The variable "start" will be used to let the ColdFusion server know which record we will want to begin at to display on the current page. The next variable is "disp", this variable will let ColdFUsion server know how many records to display per page. You can make this value any number you want.

	<!--- Start displaying with record 1 if not specified via url --->
    <CFPARAM name="start" default="1" />
    <!--- Number of records to display on a page --->
    <CFPARAM name="disp" default="5" />

The variable displayed above will make the magic appear, next let's query the database for all records:

	<!--- Fetch records --->
    <CFQUERY name="data" datasource="MyDB" />
    	SELECT MyField
        FROM MyDB
        ORDER BY MyField

Ok, now that you have your database queried and you've defined where to start and how many rows to display, we're ready to begin displaying the data on the page:

	<CFSET end=Start + disp>
	<CFIF start + disp GREATER THAN data.RecordCount>
    	<CFSET end=999>
    	<CFSET end=disp>
    <CFOUTPUT query="data" startrow="#start#" maxrows="#end#">
    	#CurrentRow#. #MyField#
		<table border="0" cellpadding="10">
            	<!--- Display prev link --->
                <CFIF start NOT EQUAL 1>
                	<CFIF start GTE disp>
                    	<CFSET prev=disp />
                       	<CFSET prevrec=start - disp />
                    	<CFSET prev=start - 1 />
                        <CFSET prevrec=1 />
                	<td><font face="wingdings">ç</font> <a href="NextN.cfm?start=#prevrec#"> Previous #prev# records</a></td>
                <!--- Display next link --->
                <CFIF end LT data.RecordCount>
                	<CFIF start + disp * 2 GTE data.RecordCount>
                    	<CFSET next=data.RecordCount - start - disp + 1 />
                    	<CFSET next=disp />
                    <a href="NextN.cfm?start=#Evaluate("start + disp")#">Next #next# records</a> <font face="wingdings">è</td>

That's it! That's all there is to adding " Previous/Next 'X' " into your website...

Questions? Comments? Email me at:

About This Tutorial
Author: Pablo Varando
Skill Level: Intermediate 
Platforms Tested: CF5
Total Views: 153,333
Submission Date: September 17, 2002
Last Update Date: August 25, 2011
All Tutorials By This Autor: 47
Discuss This Tutorial
  • Hi, This script is good and I customized as per my requirments. Only problem is that I have used this on search page and if query returns lets say 500 records, first page shows 5 recs and when I do next the query is executed again showing some more recs. I want to somehow make the query run after the 5th rec only and dont have the query return all 500 records everytime I move from one page to the next... should be a way to do this.. I am thinking of cache right now... Do you have any suggestions... This one is a must have script and I rate 5 starts for it.

  • I just wanted to express my gratitude for your most helpful script. I have been looking for and writing a script to perform this exact action. This script is very easy compared to how I was approaching the task. You are the bomb Pablo...THANK YOU!!

  • Another useful thing is to store the current location within the recordset in a session variable. This way you can always return to the same position from another page, say after editing a record.

  • Pablo, I love the script and it works great for my usual stuff, but I've got a query that uses "group" and this code keeps going through every possible combination of records. Is there a way to show the records using this code even though I'm using the "group" tag? D

  • This is a great little piece of coding. Not complicated and it works.

  • Tony, All you would likely have to do is manually append your URL variables to the anchor tags (example: Next #next# records.) If you like I have a custom tag (CF_Search_NextPrevious) that does Next/Previous links as well as page numbers. There is an attribute called "ExtraURLString" which allows you to add your own custom URL variables to be appended to the URL string. You can find it at or on the Macromedia ColdFusion Exchange.

  • I have worked though this tutorial and tried a few other, that all work well, but… I am passing some parameters to the results page. When I use the next button I loose these parameters. How do I keep the parameters in the link i.e. a form value so that it does get all the content from the database??? You can see what I mean if visit my test site at use the property search at the top and you will see how the results page changes.


Sponsored By...
Mobile App Development (IOS, Android, Cordova, Phonegap, Objective-C, Java) - Austin, Texas Mobile Apps - Touch512, LLC.