Guest User

Untitled

a guest
Dec 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. <?php
  2.  
  3. // Global Variables
  4.  
  5. $p = $_GET['p']; // Current Page
  6. $t = 9; // Total Pages
  7. $c = 1; // Context Pages
  8.  
  9. // Pagination Function
  10.  
  11. function outputLinks($page, $numberOfPages, $context) {
  12.  
  13. // Correct page variable
  14.  
  15. if (!$page || $p < 1) { $page = 1; }
  16. if ($page > $numberOfPages) { $page = $numberOfPages; }
  17.  
  18. // Set array variables
  19.  
  20. $leftArray = $midArray = $rightArray = array();
  21.  
  22. // Left Array
  23.  
  24. for ($x = 1; $x <= (1 + $context) && $x <= $numberOfPages; $x++) {
  25. array_push($leftArray, $x);
  26. }
  27.  
  28. // Right Array
  29.  
  30. for ($x = $numberOfPages - $context; $x <= $numberOfPages && $x > 0; $x++) {
  31. if (!in_array($x, $leftArray)) {
  32. array_push($rightArray, $x);
  33. }
  34. }
  35.  
  36. // Left/Right Array First/Last values to variables
  37.  
  38. $firstInLeftArray = current($leftArray);
  39. $lastInLeftArray = end($leftArray);
  40. $firstInRightArray = current($rightArray);
  41. $lastInRightArray = end($rightArray);
  42.  
  43. // Middle Array
  44.  
  45. if ($page == $firstInLeftArray || $page == $lastInRightArray) {
  46.  
  47. if (($firstInRightArray - $lastInLeftArray) > 1) {
  48. array_push($midArray, ' ... ');
  49. }
  50.  
  51. } else {
  52.  
  53. if (in_array($page, $leftArray)) {
  54. for ($x = $page; $x <= ($page + $context) && $x <= $numberOfPages; $x++) {
  55. if (!in_array($x, $leftArray) && !in_array($x, $rightArray)) {
  56. array_push($midArray, $x);
  57. }
  58. }
  59. } else if (in_array($page, $rightArray)) {
  60. for ($x = ($page - $context); $x < $firstInRightArray && $x > $lastInLeftArray; $x++) {
  61. array_push($midArray, $x);
  62. }
  63. } else {
  64. for ($x = ($page - $context); $x <= ($page + $context) && $x > 0; $x++) {
  65. if (!in_array($x, $leftArray) && !in_array($x, $rightArray)) {
  66. array_push($midArray, $x);
  67. }
  68. }
  69. }
  70.  
  71. // Middle Array First/Last values to variables
  72.  
  73. $firstInmidArray = current($midArray);
  74. $lastInmidArray = end($midArray);
  75.  
  76. // Ellipses for omitted numbers
  77.  
  78. if (($firstInmidArray - $lastInLeftArray) > 1) {
  79. array_push($leftArray, ' ... ');
  80. }
  81.  
  82. if (!empty($midArray) && ($firstInRightArray - $lastInmidArray) > 1) {
  83. array_push($midArray, ' ... ');
  84. }
  85.  
  86. }
  87.  
  88. // Display Arrays
  89.  
  90. echo 'Page '.$page.' of '.$numberOfPages.': ';
  91.  
  92. foreach ($leftArray as $value) {
  93. if ($value == ' ... ') { echo $value; } else { echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> '; }
  94. }
  95.  
  96. foreach ($midArray as $value) {
  97. if ($value == ' ... ') { echo $value; } else { echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> '; }
  98. }
  99.  
  100. foreach ($rightArray as $value) {
  101. echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> ';
  102. }
  103.  
  104. }
  105.  
  106. // Initialize document
  107.  
  108. print('<!DOCTYPE HTML><html><head><title>Pagination Function</title></head><body>');
  109.  
  110. // Generate Pagination through the outputLinks function
  111.  
  112. outputLinks($p, $t, $c);
  113.  
  114. // Close the document
  115.  
  116. print('</body></html>');
  117.  
  118. ?>
  119.  
  120. /** Model the data for pagination.
  121. */
  122. class ModelPaginator
  123. {
  124. /** Get the links for pagination.
  125. * @param page int The page number for the current page.
  126. * @param numberOfPages int The total number of paginated pages.
  127. * @param context int The number of context pages surrounding the current,
  128. * first and last pages.
  129. * @return array An array with the ranges of pages for pagination.
  130. */
  131. public function getLinks($page, $numberOfPages, $context)
  132. {
  133. // Sanitize the inputs (I am trying to follow your example here, I would
  134. // throw exceptions if the values weren't as I expected).
  135. $numberOfPages = max($numberOfPages, 1);
  136. $context = min(max($context, 0), $numberOfPages - 1);
  137. $page = min(max($page, 1), $numberOfPages);
  138.  
  139. return array_unique(
  140. array_merge(range(1, 1 + $context),
  141. range(max($page - $context, 1),
  142. min($page + $context, $numberOfPages)),
  143. range($numberOfPages - $context, $numberOfPages)));
  144. }
  145. }
  146.  
  147. /** A simple view for paginator links.
  148. */
  149. class ViewPaginatorLinks
  150. {
  151. protected $separator;
  152.  
  153. /** Construct the paginator.
  154. * @param separator string Separator between gaps.
  155. */
  156. public function __construct($separator=' ... ')
  157. {
  158. $this->separator = $separator;
  159. }
  160.  
  161. /** Write the pagination links.
  162. * @param links array The links array.
  163. * @param currentPage int The current page.
  164. */
  165. public function write($links, $currentPage)
  166. {
  167. $currentPage = min(max($currentPage, 1), end($links));
  168.  
  169. echo 'Page ' . $currentPage . ' of ' . end($links) . ': ';
  170. $previousPage = 0;
  171.  
  172. foreach ($links as $page) {
  173. if ($page !== $previousPage + 1) {
  174. $this->writeSeparator();
  175. }
  176.  
  177. $this->writeLink($page);
  178. $previousPage = $page;
  179. }
  180. }
  181.  
  182. /*******************/
  183. /* Private Methods */
  184. /*******************/
  185.  
  186. /** Write the link to the paginated page.
  187. * @param page array The page that we are writing the link for.
  188. */
  189. private function writeLink($page)
  190. {
  191. echo '<a href="?p=' . $page . '" target="_self">' . $page . '</a>' .
  192. "n";
  193. }
  194.  
  195. /// Write the separator that bridges gaps in the pagination.
  196. private function writeSeparator()
  197. {
  198. echo '<span>' . $this->separator . '</span>' . "n";
  199. }
  200. }
  201.  
  202. $numberOfPages = 29;
  203. $currentPage = 13;
  204. $context = 1;
  205.  
  206. $Model = new ModelPaginator;
  207. $View = new ViewPaginatorLinks;
  208.  
  209. $View->write($Model->getLinks($currentPage, $numberOfPages, $context),
  210. $currentPage);
  211.  
  212. Lines of Code (LOC): 117
  213. Cyclomatic Complexity / Lines of Code: 0.37
  214. Comment Lines of Code (CLOC): 34
  215. Non-Comment Lines of Code (NCLOC): 83
  216.  
  217. Maximum Line Length 120
  218.  
  219. Lines of Code (LOC): 97
  220. Cyclomatic Complexity / Lines of Code: 0.03
  221. Comment Lines of Code (CLOC): 34
  222. Non-Comment Lines of Code (NCLOC): 63
  223.  
  224. Maximum Line Length 80
  225.  
  226. if () {
  227. } else if () {
  228. }
  229.  
  230. if () {
  231. }
  232. elseif () { // elseif is equivalent to else if
  233. }
  234.  
  235. <?php
  236.  
  237. // Global Variables
  238.  
  239. $p = $_GET['p']; // Current Page
  240. $t = 9; // Total Pages
  241. $c = 1; // Context Pages
  242.  
  243. // Pagination Function
  244. function outputLinks($page, $numberOfPages, $context)
  245. {
  246. $display = array();
  247.  
  248. //Left-side pages
  249. for($i = 1; $i <= 1 + $context; $i++)
  250. {
  251. array_push($display, buildPageLink($i));
  252. }
  253.  
  254. if(($page - $context) - (1 + $context) > 1)
  255. array_push($display, "...");
  256.  
  257. //Middle pages
  258. for($i = ($page - $context); $i <= ($page + $context); $i++)
  259. {
  260. if($i > (1 + $context) && $i < ($numberOfPages - $context))
  261. array_push($display, buildPageLink($i));
  262. }
  263.  
  264. if($page + $context < $numberOfPages - $context)
  265. array_push($display, "...");
  266.  
  267. //Right-side pages
  268. for($i = $numberOfPages - $context; $i <= $numberOfPages; $i++)
  269. {
  270. array_push($display, buildPageLink($i));
  271. }
  272.  
  273. echo 'Page ' . $page . ' of ' .$numberOfPages . ': ';
  274. foreach($display as $val)
  275. {
  276. echo $val;
  277. }
  278.  
  279. }
  280.  
  281. function buildPageLink($pagenum)
  282. {
  283. return ' <a href="?p='.$pagenum.'" target="_self">'.$pagenum.'</a> ';
  284. }
  285.  
  286. // Initialize document
  287.  
  288. print('<!DOCTYPE HTML><html><head><title>Pagination Function</title></head><body>');
  289.  
  290. // Generate Pagination through the outputLinks function
  291.  
  292. outputLinks($p, $t, $c);
  293.  
  294. // Close the document
  295.  
  296. print('</body></html>');
  297.  
  298. ?>
  299.  
  300. <?php
  301. function outputLinks( $page, $numberOfPages, $context ) {
  302. # find the context pages
  303. $pages = array_filter( range( $page - $context, $page + $context ), function( $value ) use ($numberOfPages) {
  304. if ( $value <= 2 || $value >= ( $numberOfPages - 1 ) ) return false;
  305. return true;
  306. });
  307.  
  308. # are there any gaps to fill with '...':
  309. if ( isset( $pages[0] ) ) {
  310. if ( $pages[0] > 2 ) array_unshift( $pages, '...' );
  311. if ( $pages[ count( $pages ) - 1 ] < ( $numberOfPages - 2 ) ) array_push( $pages, '...' );
  312. }
  313.  
  314. # add the first and last links:
  315. array_push( $pages, $numberOfPages - 1, $numberOfPages );
  316. array_unshift( $pages, 1, 2 );
  317.  
  318. foreach ( $pages as $key => & $page ) {
  319. # if ( is_int( $page ) ) $page = '<a href="?p=' . $page . '" />';
  320. }
  321.  
  322. return $pages;
  323. }
  324. # your example with my impl:
  325. for ( $i = 1; $i <= 9; $i++ ) echo implode(' ', outputLinks( $i, 9, 1 ), PHP_EOL;
Add Comment
Please, Sign In to add comment