C3rd
PHP: Pagination | Paginator
Posted: 12 Feb 2011, 10:00am - Saturday
[caption id="attachment_520" align="alignright" width="304" caption="Pagination Screenshot"][/caption]
Last month, my classmate ask me how to create a pagination or paginator that can handle hundred of thousands of rows. And I realized that my current paginations are not good enough. It must be dynamic as possible. So, I created one. I tried this last-last week, but I didn't completed it since I'm very busy for that past few weeks. Just now, I got a chance to solve the problem.
Before that, this is my old pagination;
function paginationTest($offset = 0, $css = 'padding: 5px 5px 5px 5px; border-top: 1px #D7D7D7 dashed; text-align: right;') { global $cfgMax; global $globalProjectID; $MAXPAGE = $cfgMax; //$MAXPAGE = 2; $sqlQ = "SELECT * FROM pagetest"; $result = mysql_query( sprintf( $sqlQ ) ); $MaxValue = mysql_num_rows($result); $MaxPerPage = $MAXPAGE; $OffSet = 0; $PageCounter = 1; $TotalPages = ceil( $MaxValue / $MaxPerPage ); if ($MaxValue >= $MAXPAGE) { $rdata = '<div style="'.$css.'">Page: '; for ($i = 0; $i < $TotalPages; $i++) { $OffSet = $MaxPerPage * $i; if ($OffSet == $offset) { $rdata .= ' '.($i + 1).' '; } else { $rdata .= ' <a href="pagination.php?offset='.$OffSet.'">'.($i + 1).'</a> '; } } return $rdata.'</div>'; } else { return ' '; } }And here's my new pagination... :)
function paginationTest($page = 1, $cssContainer = 'padding: 5px 5px 5px 5px; border-top: 1px #D7D7D7 dashed; text-align: right;', $cssLink = '', $cssCurrent = '') { global $MAXPAGE; if ($page == 0) { $page = 1; } $sqlQ = "SELECT COUNT(id) FROM pagetest"; $result = mysql_query( sprintf( $sqlQ ) ); $tmp = mysql_fetch_row($result); $MaxValue = $tmp[0]; unset($tmp); mysql_free_result($result); $MaxPerPage = $MAXPAGE; $endPage = $MaxValue / $MAXPAGE; $endPage = ceil($endPage); if ($page <= 5) { $start = 1; $end = 10; } else { if (($page + 5) > $endPage) { $start = $endPage - 9; $end = $endPage; } else { $start = $page - 4; $end = $page + 5; } } $rdata = '<div style="'. $cssContainer .'">'; if ($MaxValue >= $MAXPAGE) { if ($page > 5) { /* DEFINITION: if previous is the previous page NOTES: if you like this pagination, just uncomment this and comment the other method (line 116) */ //$prevPage = $page - 1; /* DEFINITION: sif previous is the page block NOTES: if you like this pagination, just uncomment this and comment the other method (line 110) */ $prevPage = $start - 1; $rdata .= ' <a href="pagination.php?offset=0" class="'.$cssLink.'">«</a> '; $rdata .= ' <a href="pagination.php?offset='.$prevPage.'" class="'.$cssLink.'"><</a> '; } for ($i = $start; $i <= $end; $i++) { if ($page == $i) { $rdata .= ' <a href="javascript:alert(\'Page already shown!\');" class="'.$cssCurrent.'">'.$i.'</a> '; } else { $rdata .= ' <a href="pagination.php?offset='.$i.'" class="'.$cssLink.'">'.$i.'</a> '; } } if (($page + 5) < $endPage) { /* DEFINITION: if next is the next page NOTES: if you like this pagination, just uncomment this and comment the other method (line 144) */ //$nextPage = $page + 1; /* DEFINITION: if next is the page block NOTES: if you like this pagination, just uncomment this and comment the other method (line 138) */ $nextPage = $end + 1; $rdata .= ' <a href="pagination.php?offset='.$nextPage.'" class="'.$cssLink.'">></a> '; $rdata .= ' <a href="pagination.php?offset='.$endPage.'" class="'.$cssLink.'">»</a>'; } } else { $rdata = ' '; } $rdata .= '</div>'; return $rdata; }that's it... :) Hope this would help ...
- Download Source: pagination_v2.0.zip