Ask An Emperor: Query Pagination

So we have our first Ask an Emperor question from our reading audience. Lets see how it goes...

I need a simple to use component for query browsing in cf. (you know, the whole next 10, prev 10 thing). Any ideas?
You have heard before I am sure that there is more than one way to skin a cat, and the saying, as disgusting as it may be, rings true here as well. What you are talking about I believe is called query pagination, where you show a certain amount of results on each page. It really isn't difficult no matter how you do it, and its fairly simple to give your users some preferences to set as well.

First off, the two things that make this really possible is a couple attributes on some of our favorite tags. With the cfoutput tag you have an attribute called startrow and another called maxrows. These can be used to tell the cfoutput, here is the recordset I want you to use, and I want you to start at this specific record, and show the maxrows number of records. I prefer though in this case to use the cfloop tag though, because it provides you with a startrow attribute as well as an endrow attribute, which makes things a little easier, in my opinion. So with those attributes, all thats left is to figure out what the numbers are. You are going to need five variables. startRow, endRow, nextStartRow, lastStartRow and rowsPerPage. You will want to param these variables in case they dont exist. All you really need to pass from one page to another though is the startRow variable, everything else can be figured out, if you store the rowsPerPage in an application or session wide variable. Then to figure things out, its just a little logic at the top of the page.








	

	

All thats left is for you to make your links for next and back using the nextStartRow and lastStartRow variables and to plug in the startRow and endRow into your cfloop tag. Now I want to leave you with this. I definately do not suggest re-running your query on every page to plug into your cfloop! So I would do one of two things, either cache the query using the cachedwithin attribute (remember you cannot use queryparam though) or store the recordset in the session scope for use on every page. Hope that helps.

Posted on Thu. December 15, 2005 by Ryan Guill
 

1

If you want, there is also a free custom tag that might be of interest. It has many features to make it display the way you want, but best of all you can get it started with only one required attribute (the returned result count... Usually #qName.recordCount#). http://www.jeffcoughlin.com/?PG=7&FN=2&ID=1

Posted on Mon. September 15, 2008 by Jeff Coughlin #

2

Thanks Jeff!

Posted on Mon. September 15, 2008 by Ryan Guill #

3

I dont know much about programing. But, thanks all the same, lol~

Posted on Mon. September 15, 2008 by warcraft powerleveling #
Design, Photograph, Work © Ryan Guill, aDeepBlue 2013: All Rights Reserved. | Contact | RSS Feed