My Projects
Search Blog

Categories
Archives
Useful Links
Photo Albums
RSS

Powered by
BlogCFM v1.15

Vivio Technologies CFML Hosting
06 April 2007
Paging Through Record Sets without URL variables

One of my clients recently asked me to avoid the use of URL variables when paging through record sets.  He wanted the "clean" URL look.  So I converted some code I'd written to the following.

The following code uses a hidden form and some javascript to achieve the first/next/previous/last functionality.  It also displays "Showing records X - Y of Z total records"

<cfsetting enablecfoutputonly="yes">
<cfset perPage = 100>
<!--- set defaults --->
<cfparam name="session.lastStartRow" default="1">
<cfparam name="startRow" default="#session.lastStartRow#">
<cfif startRow gt qry.recordCount or startrow lt 1>
     <cfset startRow = 1>
</cfif>
<!--- calculate next and previous starting position --->
<cfset nextStartRow = startRow + perPage>
<cfset prevStartRow = startRow - perPage>
<!--- calc the last row for display purposes --->
<cfset endRow = startRow + perPage - 1>
<cfif endRow gt qry.recordCount>
     <cfset endRow = qry.recordCount>
</cfif>
<!--- save in case we leave the page and come back --->
<cfset session.lastStartRow = startRow>
<!--- the index of the first row of the LAST page is lastPageStart --->
<cfset lastPageStart = qry.recordCount - (qry.recordCount MOD perPage)>
<!--- output the hidden form used for navigation --->
<cfoutput><form name="frmPage" action="DataTable.cfm" method="post">
<input type="hidden" name="startRow" value="1" id="startRow">
</form>
<script language="Javascript">
function goPage(startRow) {
     // update the form and submit
     document.frmPage.startRow.value = startRow;
     document.frmPage.submit();
}
</script>
</cfoutput>

<!---
     Save the navigation to a variable so we can
     output it above AND Below the content easily.
--->
<cfsavecontent variable="pager"><cfoutput>
<p>
Showing records #startRow#-#endRow# of #qry.recordCount#.
<br/>
<a href="javascript:goPage(1);">First</a> &nbsp;
<cfif startRow gt 1><a href="javascript:goPage(#prevStartRow#);">Previous</a><cfelse>Previous</cfif> &nbsp;
<cfif endRow lt qry.recordCount><a href="javascript:goPage(#nextStartRow#);">Next</a><cfelse>Next</cfif> &nbsp;
<a href="javascript:goPage(#lastPageStart#)">Last</a>
</p></cfoutput></cfsavecontent>
<cfsetting enablecfoutputonly="yes">


<cfoutput>#pager#</cfoutput>

<cfoutput query="qry" maxrows="#perPage#" startrow="#startRow#">
     <!--- do stuff --->
</cfoutput>

<cfoutput>#pager#</cfoutput>


 

 

Posted by rickroot at 5:41 PM | Link | 9 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: Paging Through Record Sets without URL variables
Nice! I will have to take this and update a couple of apps that use url paging.
Posted by 7079 on April 6, 2007 at 7:05 PM

Re: Paging Through Record Sets without URL variables
oops i guess there's a little bug
Posted by dangerzone on April 18, 2007 at 9:55 AM

Re: Paging Through Record Sets without URL variables
Sorry for the mess.
here is a link where the code is posted.
http://www.nbsprinting.com/code.html
Posted by dangerzone on April 18, 2007 at 9:59 AM

Re: Paging Through Record Sets without URL variables
Hey I deleted your original comment. BlogCFM strips all HTML tags (and anything that looks like an HTML tag from comments.. so pasting in CFML code doesn't work.

One comment for you:

<cfset DSN = "#main_database#">

That's not good coding style =) The " and # signs are totally unnecessary;

<cfset DSN = main_database>
Posted by rickroot on April 18, 2007 at 12:22 PM

Re: Paging Through Record Sets without URL variables
Ummmmmm lets ponder that.....
I don?t know what you consider good coding practice rickroot, but in my case ... YEA is necessary because i dont param my DSN connection names nor do i use the dsn connection variable name in ALL my pages, I define the DSN Connection name in my Application file as so its available to ALL my pages, WHY? because if i change the connection name, im definitly not going in all my pages to find all the querys, so YEA i think its good coding practice on my part.
Posted by dangerzone on April 18, 2007 at 1:12 PM

Re: Paging Through Record Sets without URL variables
but either way, the example still works pretty cool!
:)
Posted by dangerzone on April 18, 2007 at 1:14 PM

Re: Paging Through Record Sets without URL variables
IU think you must've misunderstood me...

If your code looks like this:

<cfset DSN = "#main_database#">

It's bad coding practice. The quote symbols and pound signs are completely unnecessary, and rumor has it can actually cause performance problems.

What that statement does is evaluate the variable main_database, create a string containing the contents of that variable, and then assign that string to the variable DSN.

Now look at this code:

<cfset DSN = main_database>

This assigns the value of main_database to the variable DSN.

The end result is the same, but the original code probably does some unnecessary stuff.
Posted by rickroot on April 18, 2007 at 5:47 PM

Re: Paging Through Record Sets without URL variables
After some testing... it doesn't affect performance.

But tie quotes and pound signs are still unnecessary =)
Posted by rickroot on April 18, 2007 at 5:55 PM

Re: Paging Through Record Sets without URL variables
lol =) ok now i get it!, tks 4 the tip!
Posted by dangerzone on April 18, 2007 at 6:55 PM

Post a comment (login required)