Array Pagination Class for PHP


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

Versatile pagination class, takes in array and returns array of items to show within range of pagination. Also provides a function to display pagination links.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /************************************************************\
  4.   *
  5.   * Pagination Class
  6.   *
  7.   * Provides ability to pass an array of results and return
  8.   * a paginated trim of the results as well as provides the ability
  9.   * to conjure up the pagination links.
  10.   *
  11.   * Written by Shane Kretzmann ([email protected])
  12.   * demo can be seen at http://kretzmann.net/paginate/
  13.   *
  14.   \************************************************************/
  15.  
  16. class pagination {
  17.  
  18. // Initialize variables and set defaults
  19. var $page; // Init Current Page variable
  20. var $perPage = 10; // Default Number of Results to show on each page
  21. var $numOfPagesToShow = 10; // Default Number of pagination links to show on page.
  22. var $showFirstAndLast = true; // Set true if you would like the first and last page links to display.
  23. var $showJumpLinks = true; // Set true if you want to show jump links
  24. var $jumpNumber = 20; // If $showElipseJumpLinks is true how many pages should we jump?
  25. var $pageKey = "page"; // the string name to pass along $_GET to indicate page to display
  26.  
  27. /* Set the First, Last, Next and Prev display string defaults */
  28. var $firstString = "&laquo;&laquo; First";
  29. var $lastString = "Last &raquo;&raquo;";
  30. var $nextString = "Next &raquo;";
  31. var $prevString = "&laquo; Prev";
  32. var $jumpString = "...";
  33.  
  34.  
  35. /*
  36. * Class constructor
  37. *
  38. * @access PUBLIC
  39. * @param ARRAY $settings - optional array of pagination settings
  40. */
  41. public function __construct( $settings = '' ) {
  42. if (is_array($settings)){ // if settings array is passed, update class variables
  43. $this->perPage = (is_numeric($settings['perPage']))?$settings['perPage']:$this->perPage;
  44. $this->numOfPagesToShow = (is_numeric($settings['numOfPagesToShow']))?$settings['numOfPagesToShow']:$this->numOfPagesToShow;
  45. $this->showFirstAndLast = (isset($settings['showFirstAndLast']))?$settings['showFirstAndLast']:$this->showFirstAndLast;
  46. $this->showJumpLinks = (isset($settings['showJumpLinks']))?$settings['showJumpLinks']:$this->showJumpLinks;
  47. $this->jumpNumber = (is_numeric($settings['jumpNumber']))?$settings['jumpNumber']:$this->jumpNumber;
  48. $this->pageKey = (!empty($settings['pageKey']))?$settings['pageKey']:$this->pageKey;
  49. $this->firstString = (!empty($settings['firstString']))?$settings['firstString']:$this->firstString;
  50. $this->lastString = (!empty($settings['lastString']))?$settings['lastString']:$this->lastString;
  51. $this->nextString = (!empty($settings['nextString']))?$settings['nextString']:$this->nextString;
  52. $this->prevString = (!empty($settings['prevString']))?$settings['prevString']:$this->prevString;
  53. $this->jumpString = (!empty($settings['jumpString']))?$settings['jumpString']:$this->jumpString;
  54. }
  55. }
  56.  
  57. /*
  58. * Class destructor
  59. */
  60. public function __destruct() {
  61.  
  62. }
  63.  
  64.  
  65. /*
  66. * Name: generate()
  67. * Retrieves Pagination Link HTML
  68. *
  69. * @access PUBLIC
  70.   * @param ARRAY $array - MultiDimensional Array containing all results
  71.   * @param NUMERIC $currentPage - Optional number of the current page of results
  72.   *
  73. * @return ARRAY returns array containing results within range of pagination
  74. *
  75. */
  76. public function generate($array, $currentPage = null) {
  77.  
  78. // Get the length of the array
  79. $this->length = count($array);
  80.  
  81. // Get the number of pages
  82. $this->pages = ceil($this->length / $this->perPage);
  83.  
  84. // Check if current Page was passed or if we need to get it automatically
  85. if (isset($_GET[$this->pageKey]) && $_GET[$this->pageKey] > 0) {
  86. $this->page = $_GET[$this->pageKey]; // check for page in URI string
  87. } else if (is_int($currentPage)) {
  88. $this->page = $currentPage; // get default value start page passed in function
  89. } else {
  90. $this->page = 1; // default to page 1 if not found
  91. }
  92.  
  93. // Make sure page is in range otherwise default to last page
  94. if ($this->page > $this->pages) { $this->page = $this->pages; }
  95.  
  96. // Calculate the starting point
  97. $this->start = ceil(($this->page - 1) * $this->perPage);
  98.  
  99. // Return the part of the array we have requested
  100. return array_slice($array, $this->start, $this->perPage);
  101. }
  102.  
  103. /*
  104. * Name: links()
  105. * Retrieves Pagination Link HTML
  106. *
  107. * @access PUBLIC
  108. * @return STRING returns string containing pagination navigation
  109. *
  110. */
  111. public function links() {
  112. // Initiate the arrays and variables used in this function
  113. $leftLinks = array();
  114. $links = array();
  115. $rightLinks = array();
  116. $queryURL = '';
  117.  
  118. // ReCreate the queries passed along the url to add to the page numbering string
  119. if (count($_GET)) {
  120. foreach ($_GET as $key => $value) {
  121. if ($key != $this->pageKey) {
  122. $queryURL .= '&' . $key . '=' . $value;
  123. }
  124. }
  125. }
  126.  
  127. // If we have more then one pages
  128. if (($this->pages) > 1) {
  129. // Assign the 'previous page' link into the array if we are not on the first page
  130. if ($this->page != 1) {
  131. if ($this->showFirstAndLast) {
  132. $leftLinks[] = ' <a class="paginate firstPage" title="First Page" href="?' . $this->pageKey . '=1' . $queryURL . '">' . $this->firstString . '</a> ';
  133. }
  134. $leftLinks[] = ' <a class="paginate prevPage" title="Previous Page" href="?' . $this->pageKey . '=' . ($this->page - 1) . $queryURL . '">' . $this->prevString . '</a> ';
  135. }
  136.  
  137. // Assign all the page numbers & links to the array
  138. for ($i = 1; $i < ($this->pages + 1); $i++) {
  139. if ($this->page == $i) {
  140. $links[] = ' <span class="paginate pageSelected">' . $i . '</span> '; // If we are on the same page as the current item
  141. } else {
  142. $links[] = ' <a class="paginate" href="?' . $this->pageKey . '=' . $i . $queryURL . '">' . $i . '</a> '; // add the link to the array
  143. }
  144. }
  145.  
  146. // Assign the 'next page' if we are not on the last page
  147. if ($this->page < $this->pages) {
  148. $rightLinks[] = ' <a class="paginate nextPage" title="Next Page" href="?' . $this->pageKey . '='.($this->page + 1).$queryURL.'">' . $this->nextString . '</a> ';
  149. if ($this->showFirstAndLast) {
  150. $rightLinks[] = ' <a class="paginate lastPage" title="Last Page" href="?' . $this->pageKey . '='.($this->pages).$queryURL.'">' . $this->lastString . '</a> ';
  151. }
  152. }
  153.  
  154. // Get all of the page links for display
  155. if($this->pages > $this->numOfPagesToShow) { // enforce maximum amount of page links to show
  156. $firstPages = floor($this->page - ($this->numOfPagesToShow/2))-1;
  157.  
  158. if ($firstPages < 1) { $firstPages = 0; } // ensure it isn't a negative num
  159.  
  160. $linksOut = array_slice($links, $firstPages, $this->numOfPagesToShow+1);
  161.  
  162. if (count($linksOut) < $this->numOfPagesToShow) { // if we are near the end, make sure we are showing proper num of pages to show
  163. $firstPages = $this->pages - $this->numOfPagesToShow;
  164. $linksOut = array_slice($links, $firstPages, $this->numOfPagesToShow+1);
  165. }
  166.  
  167. if (!empty($leftLinks) && $this->showJumpLinks) {
  168. if ($this->page > $this->jumpNumber) {
  169. $leftLinks[] = ' <a class="paginate pageJump" title="Go Back ' . $this->jumpNumber . ' Pages" href="?' . $this->pageKey . '='.($this->page - $this->jumpNumber).$queryURL.'">' . $this->jumpString . '</a> ';
  170. }
  171. }
  172. if (!empty($rightLinks) && $this->showJumpLinks) {
  173. if ($this->page < ($this->pages - $this->jumpNumber)) {
  174. array_unshift($rightLinks,' <a class="paginate pageJump" title="Jump Ahead ' . $this->jumpNumber .' Pages" href="?' . $this->pageKey . '='.($this->page + $this->jumpNumber).$queryURL.'">' . $this->jumpString . '</a> ');
  175. }
  176. }
  177. } else {
  178. $linksOut = $links;
  179. }
  180.  
  181. // Push the arrays into a string and spit it back out
  182. return implode(' ', $leftLinks).implode('', $linksOut).implode(' ', $rightLinks);
  183. }
  184. return;
  185. } // end of Links Function
  186. } // end of Class
  187. ?>

URL: http://kretzmann.net/paginate/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.