php pagination navigation


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*
  2. $nr = number of results
  3. $pp = results per page
  4. $pnp = page navigation pages
  5. $pn = current page
  6. $url = base url to append navigation to
  7. $sr = starting row
  8. */
  9. function navLinks( $nr, $sr, $pp, $pnp, $pn, $url )
  10. {
  11. $pnav = '';
  12. $link = '';
  13. $start = '';
  14. $previous = '';
  15. $next = '';
  16. $end = '';
  17.  
  18. if( $pn >= 2 )
  19. {
  20. $previous .= " <a href=\"" . $url . "sr=" . ( $sr - $pp );
  21. $previous .= "&pp=" . $pp . "&cp=" . ( $pn - 1) . "\"><< Back</a> ... ";
  22. }
  23.  
  24. if( $pn < $nr and ( $pn * $pp) < $nr )
  25. {
  26. $next .= " ... <a href=\"" . $url . "sr=" . ( $sr + $pp );
  27. $next .= "&pp=" . $pp . "&cp=" . ( $pn + 1) . "\">Next >></a> ";
  28. }
  29.  
  30. if( $nr > $pp )
  31. {
  32. $tp = $nr / $pp;
  33.  
  34. if( $tp != intval( $tp ) )
  35. {
  36. $tp = intval( $tp) + 1;
  37. }
  38.  
  39. $cp = 0;
  40.  
  41. while( $cp++ < $tp )
  42. {
  43. if( ( $cp < $pn - $pnp or $cp > $pn + $pnp) and $pnp != 0 )
  44. {
  45. if( $cp == 1 )
  46. {
  47. $start .= " <a href=\"" . $url;
  48. $start .= "sr=0&";
  49. $start .= "pp=" . $pp . "&cp=1\"><< Start</a> ... ";
  50. }
  51.  
  52. if( $cp == $tp )
  53. {
  54. $end .= " ... <a href=\"" . $url;
  55. $end .= "sr=";
  56. $end .= ( $tp - 1 ) * $pp . "&pp=" . $pp . "&cp=";
  57. $end .= $tp . "\">End >></a> ... ";
  58. }
  59. }
  60. else
  61. {
  62. if( $cp == $pn )
  63. {
  64. $link .= ' <span class="selNav">[ ' . $cp . ' ]</span> ';
  65. }
  66. else
  67. {
  68. $link .= " <a href=\"" . $url;
  69. $link .= "sr=" . ( $cp - 1) * $pp;
  70. $link .= "&pp=" . $pp . "&cp=" . $cp . "\">[ $cp ]</a> ";
  71. }
  72. }
  73. }
  74.  
  75. $pnav .= $start;
  76. $pnav .= $previous;
  77. $pnav .= $link;
  78. $pnav .= $next;
  79. $pnav .= $end;
  80. }
  81.  
  82. if( $nr == 0 )
  83. {
  84. $nom = 0;
  85. }
  86. else
  87. {
  88. $nom = 1;
  89. }
  90.  
  91. $pnav .= " ... #" . ( $sr + $nom );
  92.  
  93. if( $pp > 1 )
  94. {
  95. $pnav .= " - ";
  96.  
  97. if( $sr + $nom + $pp < $nr )
  98. {
  99. $pnav .= ( $sr + $nom + $pp ) - 1;
  100. }
  101. else
  102. {
  103. $pnav .= $nr;
  104. }
  105. }
  106.  
  107. $pnav .= " of " . $nr . " ";
  108.  
  109. return $pnav;
  110. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.