Posted By

pumpkinthehead on 08/22/12


Tagged

pagination


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


Pagination Example


 / Published in: PHP
 

A quick pagination framework.

  1. <span style="float: right;">
  2. <a href="myContacts.php?page=1">&lt;&lt;</a>
  3. <a href="myContacts.php?page=<?=$_SESSION['contactPage']-1;?>">&lt;</a>
  4. <?
  5. //Get contact pages for this screen.
  6. //Set page upper and lower limits.
  7. if($_SESSION['contactPage'] > 5)
  8. {
  9.  
  10. if($_SESSION['contactPage'] + 4 <= $pages)
  11. {
  12. $lowerLimit = $_SESSION['contactPage'] - 4;
  13. $upperLimit = $_SESSION['contactPage'] + 4;
  14. }
  15. else
  16. {
  17. $lowerLimit = $pages - 8;
  18. $upperLimit = $pages;
  19. }
  20. }
  21. else
  22. {
  23. $lowerLimit = 1;
  24. $upperLimit = 9;
  25. }
  26.  
  27. //Displays previous ellipsis link if applicable.
  28. if($lowerLimit > 1)
  29. {
  30. if($_SESSION['contactPage'] >= 11)
  31. {
  32. $backEllipsis = $_SESSION['contactPage'] - 10;
  33. }
  34. else
  35. {
  36. $backEllipsis = 1;
  37. }
  38. ?>
  39. <a href="myContacts.php?page=<?=$backEllipsis?>">
  40. ...
  41. </a>
  42. <?
  43. }
  44.  
  45. //Display 9 pages with current page in the middle.
  46. for($pageCount = $lowerLimit; $pageCount <= $upperLimit; $pageCount ++)
  47. {
  48. //Display link for pages not equal to current page.
  49. if($pageCount == $_SESSION['contactPage'])
  50. {
  51. echo $pageCount;
  52. }
  53. else
  54. {
  55. ?>
  56. <a href="myContacts.php?page=<?=$pageCount?>">
  57. <?=$pageCount?>
  58. </a>
  59. <?
  60. }
  61. }
  62.  
  63. //Displays after ellipsis link if applicable.
  64. if($upperLimit < $pages)
  65. {
  66. if($_SESSION['contactPage'] + 10 <= $pages)
  67. {
  68. $forwardEllipsis = $_SESSION['contactPage'] + 10;
  69. }
  70. else
  71. {
  72. $forwardEllipsis = $pages;
  73. }
  74. ?>
  75. <a href="myContacts.php?page=<?=$forwardEllipsis?>">
  76. ...
  77. </a>
  78. <?
  79. }
  80. ?>
  81. <a href="myContacts.php?page=<?=$_SESSION['contactPage']+1;?>">&gt;</a>
  82. <a href="myContacts.php?page=<?=$pages?>">&gt;&gt;</a>
  83. </span>

Report this snippet  

You need to login to post a comment.