NYCPHP Meetup

NYPHP.org

[nycphp-talk] Paging Strategies

Brent Baisley brenttech at gmail.com
Thu Oct 18 17:20:07 EDT 2007


You hardly need a library for figuring out pagination. A very simple
formula tells you how many "pages" you have.
$itemsPerPage = 10;
$totalRecords = 52; //Result of SELECT FOUND_ROWS()
$pages = ceil($totalRecords/$itemsPerPage);

Based on those numbers, you know you have 6 pages of data. You can
then create a loop to generate the links with the proper parameter(s),
however you want to format them.
For example:
link.php?page=3&itemsperpage=10

Would make the following query:
SELECT * FROM table LIMIT ($_GET['page']-1)*10,10

I may be off by 1 on that. Hope that helps.

On 10/18/07, Brent Baisley <brenttech at gmail.com> wrote:
> You don't need to do an extra count query. If you are using MySQL,
> just add SQL_CALC_FOUND_ROWS to your select query.
> SELECT SQL_CALC_FOUND_ROWS * FROM ...
>
> You can then run SELECT FOUND_ROWS() to get the total rows without any
> limits. It's still 2 queries, but the second one is essentially free.
>
>
> On 10/18/07, Cliff Hirsch <cliff at pinestream.com> wrote:
> > > 1) TWO-QUERY APPROACH
> > > on every page view:
> > FAILSAFE. An extra count query, but no worries.
> >
> > > 3) ONE QUERY, CACHE EVERYTHING
> > What if the count changes between pages view? What if there are millions of
> > records -- awfully big fetch. What if you change the application down the
> > road, which creates the potential for changes in row count between views?
> > Lot's of state info to think about....
> >
> > My two cents...
> > Cliff
> >
> >
> > _______________________________________________
> > New York PHP Community Talk Mailing List
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> > NYPHPCon 2006 Presentations Online
> > http://www.nyphpcon.com
> >
> > Show Your Participation in New York PHP
> > http://www.nyphp.org/show_participation.php
> >
>



More information about the talk mailing list