Array Pagination


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

very easy to use pagination class, see website for comments


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /************************************************************\
  4.   *
  5.   * PHP Array Pagination Copyright 2007 - Derek Harvey
  6.   * www.lotsofcode.com
  7.   *
  8.   * This file is part of PHP Array Pagination .
  9.   *
  10.   * PHP Array Pagination is free software; you can redistribute it and/or modify
  11.   * it under the terms of the GNU General Public License as published by
  12.   * the Free Software Foundation; either version 2 of the License, or
  13.   * (at your option) any later version.
  14.   *
  15.   * PHP Array Pagination is distributed in the hope that it will be useful,
  16.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18.   * GNU General Public License for more details.
  19.   *
  20.   * You should have received a copy of the GNU General Public License
  21.   * along with PHP Array Pagination ; if not, write to the Free Software
  22.   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23.   *
  24.   \************************************************************/
  25.  
  26. class pagination
  27. {
  28. var $page = 1; // Current Page
  29. var $perPage = 10; // Items on each page, defaulted to 10
  30. var $showFirstAndLast = false; // if you would like the first and last page options.
  31.  
  32. function generate($array, $perPage = 10)
  33. {
  34. // Assign the items per page variable
  35. if (!empty($perPage))
  36. $this->perPage = $perPage;
  37.  
  38. // Assign the page variable
  39. if (!empty($_GET['page'])) {
  40. $this->page = $_GET['page']; // using the get method
  41. } else {
  42. $this->page = 1; // if we don't have a page number then assume we are on the first page
  43. }
  44.  
  45. // Take the length of the array
  46. $this->length = count($array);
  47.  
  48. // Get the number of pages
  49. $this->pages = ceil($this->length / $this->perPage);
  50.  
  51. // Calculate the starting point
  52. $this->start = ceil(($this->page - 1) * $this->perPage);
  53.  
  54. // Return the part of the array we have requested
  55. return array_slice($array, $this->start, $this->perPage);
  56. }
  57.  
  58. function links()
  59. {
  60. // Initiate the links array
  61. $plinks = array();
  62. $links = array();
  63. $slinks = array();
  64.  
  65. // Concatenate the get variables to add to the page numbering string
  66. if (count($_GET)) {
  67. $queryURL = '';
  68. foreach ($_GET as $key => $value) {
  69. if ($key != 'page') {
  70. $queryURL .= '&'.$key.'='.$value;
  71. }
  72. }
  73. }
  74.  
  75. // If we have more then one pages
  76. if (($this->pages) > 1)
  77. {
  78. // Assign the 'previous page' link into the array if we are not on the first page
  79. if ($this->page != 1) {
  80. if ($this->showFirstAndLast) {
  81. $plinks[] = ' <a href="?page=1'.$queryURL.'">&laquo;&laquo; First </a> ';
  82. }
  83. $plinks[] = ' <a href="?page='.($this->page - 1).$queryURL.'">&laquo; Prev</a> ';
  84. }
  85.  
  86. // Assign all the page numbers & links to the array
  87. for ($j = 1; $j < ($this->pages + 1); $j++) {
  88. if ($this->page == $j) {
  89. $links[] = ' <a class="selected">'.$j.'</a> '; // If we are on the same page as the current item
  90. } else {
  91. $links[] = ' <a href="?page='.$j.$queryURL.'">'.$j.'</a> '; // add the link to the array
  92. }
  93. }
  94.  
  95. // Assign the 'next page' if we are not on the last page
  96. if ($this->page < $this->pages) {
  97. $slinks[] = ' <a href="?page='.($this->page + 1).$queryURL.'"> Next &raquo; </a> ';
  98. if ($this->showFirstAndLast) {
  99. $slinks[] = ' <a href="?page='.($this->pages).$queryURL.'"> Last &raquo;&raquo; </a> ';
  100. }
  101. }
  102.  
  103. // Push the array into a string using any some glue
  104. return implode(' ', $plinks).implode($this->implodeBy, $links).implode(' ', $slinks);
  105. }
  106. return;
  107. }
  108. }
  109. ?>
  110.  
  111. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  112. <html>
  113. <head>
  114. <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  115. <title>PHP Array Pagination</title>
  116. <style>
  117. <!--
  118. body {
  119. font-family: Tahoma, Verdana, Arial, Sans-serif;
  120. font-size: 11px;
  121. }
  122. hr {
  123. border: 1px #ccc;
  124. border-style: none none solid none;
  125. margin: 20px 0;
  126. }
  127. a {
  128. color: #333;
  129. text-decoration: none;
  130. }
  131. a:hover {
  132. text-decoration: underline;
  133. }
  134. a.selected {
  135. font-weight: bold;
  136. text-decoration: underline;
  137. }
  138. .numbers {
  139. line-height: 20px;
  140. word-spacing: 4px;
  141. }
  142. //-->
  143. </style>
  144. </head>
  145. <body>
  146. <h1>PHP Array Pagination</h1>
  147. <hr />
  148. <?php
  149. // Include the pagination class
  150. include 'pagination.class.php';
  151. // Create the pagination object
  152. $pagination = new pagination;
  153.  
  154. // some example data
  155. foreach (range(1, 100) as $value) {
  156. $products[] = array(
  157. 'Product' => 'Product '.$value,
  158. 'Price' => rand(100, 1000),
  159. );
  160. }
  161.  
  162. // If we have an array with items
  163. if (count($products)) {
  164. // Parse through the pagination class
  165. $productPages = $pagination->generate($products, 10);
  166. // If we have items
  167. if (count($productPages) != 0) {
  168. // Create the page numbers
  169. echo $pageNumbers = '<div class="numbers">'.$pagination->links().'</div>';
  170. // Loop through all the items in the array
  171. foreach ($productPages as $productArray) {
  172. // Show the information about the item
  173. echo '<p><b>'.$productArray['Product'].'</b> &nbsp; &pound;'.$productArray['Price'].'</p>';
  174. }
  175. // print out the page numbers beneath the results
  176. echo $pageNumbers;
  177. }
  178. }
  179. ?>
  180. <hr />
  181. <p><a href="http://www.lotsofcode.com/php/projects/php-array-pagination" target="_blank">PHP Array Pagination</a> provided by <a href="http://www.lotsofcode.com/" target="_blank">Lots of Code</a></p>
  182. </body>
  183. </html>

URL: http://www.lotsofcode.com/php/php-array-pagination.htm

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.