Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Global Variables
- $p = $_GET['p']; // Current Page
- $t = 9; // Total Pages
- $c = 1; // Context Pages
- // Pagination Function
- function outputLinks($page, $numberOfPages, $context) {
- // Correct page variable
- if (!$page || $p < 1) { $page = 1; }
- if ($page > $numberOfPages) { $page = $numberOfPages; }
- // Set array variables
- $leftArray = $midArray = $rightArray = array();
- // Left Array
- for ($x = 1; $x <= (1 + $context) && $x <= $numberOfPages; $x++) {
- array_push($leftArray, $x);
- }
- // Right Array
- for ($x = $numberOfPages - $context; $x <= $numberOfPages && $x > 0; $x++) {
- if (!in_array($x, $leftArray)) {
- array_push($rightArray, $x);
- }
- }
- // Left/Right Array First/Last values to variables
- $firstInLeftArray = current($leftArray);
- $lastInLeftArray = end($leftArray);
- $firstInRightArray = current($rightArray);
- $lastInRightArray = end($rightArray);
- // Middle Array
- if ($page == $firstInLeftArray || $page == $lastInRightArray) {
- if (($firstInRightArray - $lastInLeftArray) > 1) {
- array_push($midArray, ' ... ');
- }
- } else {
- if (in_array($page, $leftArray)) {
- for ($x = $page; $x <= ($page + $context) && $x <= $numberOfPages; $x++) {
- if (!in_array($x, $leftArray) && !in_array($x, $rightArray)) {
- array_push($midArray, $x);
- }
- }
- } else if (in_array($page, $rightArray)) {
- for ($x = ($page - $context); $x < $firstInRightArray && $x > $lastInLeftArray; $x++) {
- array_push($midArray, $x);
- }
- } else {
- for ($x = ($page - $context); $x <= ($page + $context) && $x > 0; $x++) {
- if (!in_array($x, $leftArray) && !in_array($x, $rightArray)) {
- array_push($midArray, $x);
- }
- }
- }
- // Middle Array First/Last values to variables
- $firstInmidArray = current($midArray);
- $lastInmidArray = end($midArray);
- // Ellipses for omitted numbers
- if (($firstInmidArray - $lastInLeftArray) > 1) {
- array_push($leftArray, ' ... ');
- }
- if (!empty($midArray) && ($firstInRightArray - $lastInmidArray) > 1) {
- array_push($midArray, ' ... ');
- }
- }
- // Display Arrays
- echo 'Page '.$page.' of '.$numberOfPages.': ';
- foreach ($leftArray as $value) {
- if ($value == ' ... ') { echo $value; } else { echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> '; }
- }
- foreach ($midArray as $value) {
- if ($value == ' ... ') { echo $value; } else { echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> '; }
- }
- foreach ($rightArray as $value) {
- echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> ';
- }
- }
- // Initialize document
- print('<!DOCTYPE HTML><html><head><title>Pagination Function</title></head><body>');
- // Generate Pagination through the outputLinks function
- outputLinks($p, $t, $c);
- // Close the document
- print('</body></html>');
- ?>
- /** Model the data for pagination.
- */
- class ModelPaginator
- {
- /** Get the links for pagination.
- * @param page int The page number for the current page.
- * @param numberOfPages int The total number of paginated pages.
- * @param context int The number of context pages surrounding the current,
- * first and last pages.
- * @return array An array with the ranges of pages for pagination.
- */
- public function getLinks($page, $numberOfPages, $context)
- {
- // Sanitize the inputs (I am trying to follow your example here, I would
- // throw exceptions if the values weren't as I expected).
- $numberOfPages = max($numberOfPages, 1);
- $context = min(max($context, 0), $numberOfPages - 1);
- $page = min(max($page, 1), $numberOfPages);
- return array_unique(
- array_merge(range(1, 1 + $context),
- range(max($page - $context, 1),
- min($page + $context, $numberOfPages)),
- range($numberOfPages - $context, $numberOfPages)));
- }
- }
- /** A simple view for paginator links.
- */
- class ViewPaginatorLinks
- {
- protected $separator;
- /** Construct the paginator.
- * @param separator string Separator between gaps.
- */
- public function __construct($separator=' ... ')
- {
- $this->separator = $separator;
- }
- /** Write the pagination links.
- * @param links array The links array.
- * @param currentPage int The current page.
- */
- public function write($links, $currentPage)
- {
- $currentPage = min(max($currentPage, 1), end($links));
- echo 'Page ' . $currentPage . ' of ' . end($links) . ': ';
- $previousPage = 0;
- foreach ($links as $page) {
- if ($page !== $previousPage + 1) {
- $this->writeSeparator();
- }
- $this->writeLink($page);
- $previousPage = $page;
- }
- }
- /*******************/
- /* Private Methods */
- /*******************/
- /** Write the link to the paginated page.
- * @param page array The page that we are writing the link for.
- */
- private function writeLink($page)
- {
- echo '<a href="?p=' . $page . '" target="_self">' . $page . '</a>' .
- "n";
- }
- /// Write the separator that bridges gaps in the pagination.
- private function writeSeparator()
- {
- echo '<span>' . $this->separator . '</span>' . "n";
- }
- }
- $numberOfPages = 29;
- $currentPage = 13;
- $context = 1;
- $Model = new ModelPaginator;
- $View = new ViewPaginatorLinks;
- $View->write($Model->getLinks($currentPage, $numberOfPages, $context),
- $currentPage);
- Lines of Code (LOC): 117
- Cyclomatic Complexity / Lines of Code: 0.37
- Comment Lines of Code (CLOC): 34
- Non-Comment Lines of Code (NCLOC): 83
- Maximum Line Length 120
- Lines of Code (LOC): 97
- Cyclomatic Complexity / Lines of Code: 0.03
- Comment Lines of Code (CLOC): 34
- Non-Comment Lines of Code (NCLOC): 63
- Maximum Line Length 80
- if () {
- } else if () {
- }
- if () {
- }
- elseif () { // elseif is equivalent to else if
- }
- <?php
- // Global Variables
- $p = $_GET['p']; // Current Page
- $t = 9; // Total Pages
- $c = 1; // Context Pages
- // Pagination Function
- function outputLinks($page, $numberOfPages, $context)
- {
- $display = array();
- //Left-side pages
- for($i = 1; $i <= 1 + $context; $i++)
- {
- array_push($display, buildPageLink($i));
- }
- if(($page - $context) - (1 + $context) > 1)
- array_push($display, "...");
- //Middle pages
- for($i = ($page - $context); $i <= ($page + $context); $i++)
- {
- if($i > (1 + $context) && $i < ($numberOfPages - $context))
- array_push($display, buildPageLink($i));
- }
- if($page + $context < $numberOfPages - $context)
- array_push($display, "...");
- //Right-side pages
- for($i = $numberOfPages - $context; $i <= $numberOfPages; $i++)
- {
- array_push($display, buildPageLink($i));
- }
- echo 'Page ' . $page . ' of ' .$numberOfPages . ': ';
- foreach($display as $val)
- {
- echo $val;
- }
- }
- function buildPageLink($pagenum)
- {
- return ' <a href="?p='.$pagenum.'" target="_self">'.$pagenum.'</a> ';
- }
- // Initialize document
- print('<!DOCTYPE HTML><html><head><title>Pagination Function</title></head><body>');
- // Generate Pagination through the outputLinks function
- outputLinks($p, $t, $c);
- // Close the document
- print('</body></html>');
- ?>
- <?php
- function outputLinks( $page, $numberOfPages, $context ) {
- # find the context pages
- $pages = array_filter( range( $page - $context, $page + $context ), function( $value ) use ($numberOfPages) {
- if ( $value <= 2 || $value >= ( $numberOfPages - 1 ) ) return false;
- return true;
- });
- # are there any gaps to fill with '...':
- if ( isset( $pages[0] ) ) {
- if ( $pages[0] > 2 ) array_unshift( $pages, '...' );
- if ( $pages[ count( $pages ) - 1 ] < ( $numberOfPages - 2 ) ) array_push( $pages, '...' );
- }
- # add the first and last links:
- array_push( $pages, $numberOfPages - 1, $numberOfPages );
- array_unshift( $pages, 1, 2 );
- foreach ( $pages as $key => & $page ) {
- # if ( is_int( $page ) ) $page = '<a href="?p=' . $page . '" />';
- }
- return $pages;
- }
- # your example with my impl:
- for ( $i = 1; $i <= 9; $i++ ) echo implode(' ', outputLinks( $i, 9, 1 ), PHP_EOL;
Add Comment
Please, Sign In to add comment