Posted By

kendsnyder on 01/18/11


Tagged

sql html Pager


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

pytheas


SQL results pager HTML


 / Published in: PHP
 

  1. function getResultPaging($perPage, $currPage, $total) {
  2. $paging = new stdClass;
  3.  
  4. // get text
  5. $startRec = ($perPage * ($currPage - 1)) + 1;
  6. $endRec = min($total, $perPage * $currPage);
  7. $totalPages = ceil($total / $perPage);
  8. $paging->text = "Viewing records $startRec–$endRec of $total";
  9. $paging->links = array();
  10.  
  11. // do we have a search string to append to each paging link?
  12. if (($searchName = trim(@$_REQUEST['search']))) {
  13. $searchString = "&search=" . rawurlencode($searchName);
  14. }
  15. else {
  16. $searchString = '';
  17. }
  18. $_SESSION['last-search'] = '?page=' . $currPage . $searchString;
  19.  
  20. // get pages that should be listed
  21. if ($currPage > 5 && $totalPages > 10) {
  22. $posFromLast = $totalPages - $currPage;
  23. if ($posFromLast >= 5) {
  24. $pages = range($currPage - 4, $currPage + 5);
  25. }
  26. else {
  27. $pages = range($currPage - 9 + $posFromLast, $totalPages);
  28. }
  29. }
  30. else {
  31. $pages = range(1, $totalPages > 10 ? 10 : $totalPages);
  32. }
  33.  
  34. // prev link
  35. $paging->links[] = $currPage > 1 ?
  36. '<a class="item prev-next prev" href="?page='.($currPage-1).$searchString.'"><span class="quo">&laquo;</span> Prev</a>' :
  37. '<span class="item prev-next prev inactive"><span class="quo">&laquo;</span> Prev</span>';
  38.  
  39. if ($pages[0] != 1) {
  40. $paging->links[] = '<span class="item elipses">...</span>';
  41. }
  42.  
  43. // regular links
  44. foreach ($pages as $page) {
  45. $paging->links[] = $currPage == $page ?
  46. '<span class="item inactive current">'.$page.'</span>' :
  47. '<a class="item" href="?page='.$page.$searchString.'">'.$page.'</a>';
  48. }
  49.  
  50. if ($pages[count($pages) - 1] != $totalPages) {
  51. $paging->links[] = '<span class="item elipses">...</span>';
  52. }
  53.  
  54. // next link
  55. $paging->links[] = $currPage < $totalPages ?
  56. '<a class="item prev-next prev" href="?page='.($currPage+1).$searchString.'">Next <span class="quo">&raquo;</span></a>' :
  57. '<span class="item prev-next prev inactive">Next <span class="quo">&raquo;</span></span>';
  58.  
  59. return $paging;
  60. }

Report this snippet  

You need to login to post a comment.