A simple next / prev navigation
You probably encountered this lot's of times, the need for a simple next / prev function to page thru your recordset. If not... uhm.. yeah.. ok.. continue. What I created is a simple function that will calculate the x pages to view and will give you back some variables that you can use to create your own navigation set. First let me show you the function. <!--- Next x record function --->
<cffunction name="nextXpage" access="public" returntype="struct" hint="Next x page function" output="No">
<cfargument name="recordcount" required="Yes" default="0" type="numeric" hint="The total numbers of records / values">
<cfargument name="page" required="Yes" type="numeric" default="0" hint="The page number that you are currently on. (for example URL.page)">
<cfargument name="maxrows" required="No" type="numeric" default="10" hint="The maximum records / values to be displayed per page">
<!--- Define the return structure --->
<cfset var nextXpage = structNew()>
<!--- Calculate how much page are available according to the maxrows displayed --->
<cfset nextXpage.pagecount = CEILING(arguments.recordcount / arguments.maxrows)>
<!--- Set the maximum rows to be displayed --->
<cfset nextXpage.maxrows = arguments.maxrows>
<!--- Set the currentpage --->
<cfset nextXpage.currentpage = arguments.page>
<!--- Set the currentpage --->
<cfset nextXpage.recordcount = arguments.recordcount>
<!--- Set the startrow value --->
<cfif arguments.page eq 1>
<cfset nextXpage.startrow = 1>
<cfelse>
<cfset nextXpage.startrow = (arguments.page * arguments.maxrows) - (arguments.maxrows -1)>
</cfif>
<!--- Define the previous and next values. --->
<!--- Next values --->
<cfif arguments.page neq nextXpage.pagecount>
<cfset nextXpage.next = true>
<cfset nextXpage.nextpage = arguments.page + 1>
<cfelse>
<cfset nextXpage.next = false>
<cfset nextXpage.nextpage = 1>
</cfif>
<!--- Previous values --->
<cfif arguments.page eq 1>
<cfset nextXpage.previous = false>
<cfset nextXpage.previouspage = 1>
<cfelse>
<cfset nextXpage.previous = true>
<cfset nextXpage.previouspage = arguments.page - 1>
</cfif>
<!--- Return the structure --->
<cfreturn nextXpage>
</cffunction>
As you can see the function needs 2 values to work and one is and those are the total numbers of records you have,the page you are on (default 1) and the maximum rows you would like to show on your page (default 10). Put this functionbetween cfcomponent tags and save as functions.cfc. Below is the code to make this all work. <cfparam name="URL.page" default="1">
<!--- Determine the navigation parameters --->
<cfset functions = createobject("component","functions")>
<cfset nav = functions.nextXpage(somequery.recordcount,URL.page,15)>
The function will now return a structure with variables you can use to build your own navigation for paging thru the recordset. I normally use the following to let the user see on which page he/she is and how many pages there still are. <cfoutput>
#nav.recordcount# items (page #nav.currentpage# of #nav.pagecount#)
<cfif nav.previous>
<a href="somepage.cfm?page=#nav.previouspage#">Previous</a>
<cfelse>
Previous
</cfif>
<cfif nav.next>
<a href="somepage.cfm?page=#nav.nextpage#">Next</a>
<cfelse>
Next
</cfif>
</cfoutput>
As you can see, a simple way of paging a recordset. There are probably much better ways of doing this but until my favorite server MSSQL (still 2000 i'm afraid) doesn't include paging query's I keep on using this bit of code to browse my recordsets
Oeps.. forgot a major piece of code to let all this work correctly!!! You have to set your cfoutput tag with the following settings.
<cfoutput query="yourquery" startrow="#nav.startrow#" maxrows="#nav.maxrows#">
<!--- Output here --->
</cfoutput> 11036 viewed | 1 opinion(s) | del.icio.us | Digg it | Tjarko @ 03/07/07 15:40 cet



