NYCPHP Meetup

NYPHP.org

[nycphp-talk] Paging Strategies

Dell Sala dell at sala.ca
Fri Oct 19 00:07:22 EDT 2007


>> From: "Brent Baisley" <brenttech at gmail.com>
>>
>> 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.
>
> On Oct 18, 2007, at 5:27 PM, Ben Sgro ((ProjectSkyLine)) wrote:
> Sure, I guess that's a fine way to do it.
> I'll just toss it into a class for reuseability.


Here's my version of such a class:
http://dev.dellsala.com/source/Pager.phps

An example of how it might be used:

$pager = new Pager();
$pager->setItemsPerPage(10);
$pager->setCurrentPage(2);	// from $_POST or $_GET

// generate your query LIMIT
$sqlLimit = "LIMIT {$pager->getItemOffset()}, {$pager->getItemsPerPage 
()}";

$pager->setItemCount(125);	// from SELECT FOUND_ROWS(). thanks brent!

// output the paging interface
?>
<p>Page <?php echo $pager->getCurrentPage(); ?> of <? echo $pager- 
 >getPageCount(); ?></p>
<a href="search.php?page=<?php echo $pager->getPreviousPage(); ? 
 >">Previous</a>
<a href="search.php?page=<?php echo $pager->getNextPage(); ?>">Next</a>

-- Dell




More information about the talk mailing list