I search a pagination script/class/helper in PHP for an Array of data, not for SQL statements. Someone know a solution?
+1
A:
Off the top of my head you could have look at the LimitIterator which is part of the SPL.
flungabunga
2009-11-02 10:54:58
+1
A:
If you're looking for pagination of a collection that doesn't involve SQL you may have better luck implementing pagination client side with a large collection of results. I'm just thinking that if you have a single block of results of php data then you don't really want to be reloading it again and again - usually sql would be keeping track of the next/previous results.
This jquery pagination plugin might be useful?
Steerpike
2009-11-02 10:57:49
I tried it with the jquery pagination plugin but it failed with a lot of data which I have in my case
gustavgans
2009-11-02 11:32:06
A:
basic pagination you can tweak to whatever you need. the second code snipped would be the part in your view. You could easily take this and make it more generic to be used whenever you needed it.
// !!! make sure that page is set as a url param
if( !isset( $_GET[ 'page' ] )){
$_GET[ 'page' ] = 0;
}
// find out how many rows there are total
$totalRows = mysql_query( "SELECT COUNT(*) FROM table" );
$totalRows = mysql_num_rows( $totalRows );
// find out how many pages there will be
$numPages = floor( $totalRows / $perPage );
$perPage = 15;
$offset = floor( $_GET[ 'page' ] * $perPage );
// get just the data you need for the page you are on
$pageData = mysql_query( "SELECT * FROM table LIMIT $perPage OFFSET $offset" );
then on the page to create the links
// get the current url to replace
for ($i=0; $i <= $numPages; $i++) {
if( $_GET[ 'page'] != $i ){
echo '<a href="/yourpage.php?page=' . $i + 1 . '">page '.( $i+1 ).'</a>';
}else{
echo "page " . ( $i+1 );
}
}
David Morrow
2010-02-24 16:28:19