开发者

Need code for pagination in PHP? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying 开发者_如何学JAVAthis question so that it can be reopened, visit the help center. Closed 10 years ago.

I search a pagination script/class/helper in PHP for an Array of data, not for SQL statements. Someone know a solution?


Off the top of my head you could have look at the LimitIterator which is part of the SPL.


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?


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 );
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜