One of the main things all developers come to in applications is pagination and the best way we found to do this is by Raymond Camden which covers how to create next and previous buttons for your content using the URL variable.

As we have extended what Raymond Camden has done we will go through all the code again to make sure you get the full picture.

First part is to get our data, which most of the time will be a database or as the second data example shows a queryNew which is a way of creating data in a query format without the need of a database query.

<cfquery name=”data” datasource=”data_dsn”>

SELECT *
FROM data

</cfquery>

OR

<cfset data = queryNew(“id,name,age,active”,”integer,varchar,integer,bit”)>

<cfloop index=”x” from=”1″ to=”22″>
<cfset queryAddRow(data)>

<cfset querySetCell(data,”id”,x)>
<cfset querySetCell(data,”name”,”User #x#”)>
<cfset querySetCell(data,”age”,randRange(20,90))>

<cfset querySetCell(data,”active”,false)>


</cfloop>

Next we want to set the number of records per page we would like to show, for this example code we are using 10:

<cfset perpage = 10>

Now we create the code for the URL variables which counts the records in the data we have got from our database / query. We also set the default of the variable URL.start to 1 as we want to start from the first record and not 0.

<cfparam name=”url.start” default=”1″>

<cfif not isNumeric(url.start) or url.start lt 1 or url.start gt data.recordCount or round(url.start) neq url.start>
<cfset url.start = 1>
</cfif>

The output of the data, you will notice on our <cfoutput> tag we have startrow & maxrows which sets what records are displayed.

<cfoutput query=”data” startrow=”#url.start#” maxrows=”#perpage#”>
#currentrow#) #name#<br />
</cfoutput>

And now this is when the code that Raymond Camden created changes a little to add in a new feature, page numbering. We first have a previous link which has an if statement around it to see if its a link or not and enable the <a> tag or have no link on it. This can be done different ways but to keep with Raymond Camden’s code we will leave it as it is. Now the new part you can see within the commented area (<!— Start Page Number —> CODE <!— End Page Number —>) we set 2 variables one for a page count as we need to have a number of pages that will be listed. The next one is page link, this is to fit in with the rest of the code and add the number that will need to be added within the link to view the numbered page. Next is a loop to go through the number of pages with the link and a small bit of VERY simple maths to add up the next page through the loop.

<p align=”center”>

[

<cfif url.start gt 1>

<cfset link = cgi.script_name & “?start=” & (url.start – perpage)>

<cfoutput><a href=”#link#”>Previous Page</a></cfoutput>

<cfelse>

Previous Page

</cfif>

/

<!— Start Page Number —>

<cfset pageCount = 1>
<cfset pageLink = 1>

<cfset totalPages = Ceiling(rs_getReports.recordCount / perpage)>

<cfloop index=”c” from=”1″ to=”#totalPages#”>

<cfoutput>

<a href=”?start=#pageLink#”>#pageCount#</a>

</cfoutput>

<cfset pageCount = pageCount + 1>

<cfset pageLink = pageLink + perpage>

</cfloop>

<!— End Page Number —>

/

<cfif (url.start + perpage – 1) lt rs_getReports.recordCount>

<cfset link = cgi.script_name & “?start=” & (url.start + perpage)>

<cfoutput><a href=”#link#”>Next Page</a></cfoutput>

<cfelse>

Next Page

</cfif>

]

</p>

Thats it, you will now have something that looks like this:

Paging
Base code taken from Raymond Camden’s coldfusionjedi.com :

http://www.coldfusionjedi.com/index.cfm/2006/4/24/ColdFusion-and-Pagination

Leave a Reply

Your email address will not be published. Required fields are marked *

About Host Media

We have been hosting websites since 2002 and are always moving forward. All articles written under the Host Media author are created by the team who support our customers.

Category

ColdFusion/Railo & MySQL

Tags

, , ,