/ Published in: PHP
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /************************************************************\ * * Pagination Class * * Provides ability to pass an array of results and return * a paginated trim of the results as well as provides the ability * to conjure up the pagination links. * * Written by Shane Kretzmann ([email protected]) * demo can be seen at http://kretzmann.net/paginate/ * \************************************************************/ class pagination { // Initialize variables and set defaults var $page; // Init Current Page variable var $perPage = 10; // Default Number of Results to show on each page var $numOfPagesToShow = 10; // Default Number of pagination links to show on page. var $showFirstAndLast = true; // Set true if you would like the first and last page links to display. var $showJumpLinks = true; // Set true if you want to show jump links var $jumpNumber = 20; // If $showElipseJumpLinks is true how many pages should we jump? var $pageKey = "page"; // the string name to pass along $_GET to indicate page to display /* Set the First, Last, Next and Prev display string defaults */ var $firstString = "«« First"; var $lastString = "Last »»"; var $nextString = "Next »"; var $prevString = "« Prev"; var $jumpString = "..."; /* * Class constructor * * @access PUBLIC * @param ARRAY $settings - optional array of pagination settings */ public function __construct( $settings = '' ) { $this->numOfPagesToShow = (is_numeric($settings['numOfPagesToShow']))?$settings['numOfPagesToShow']:$this->numOfPagesToShow; $this->showFirstAndLast = (isset($settings['showFirstAndLast']))?$settings['showFirstAndLast']:$this->showFirstAndLast; $this->showJumpLinks = (isset($settings['showJumpLinks']))?$settings['showJumpLinks']:$this->showJumpLinks; $this->jumpNumber = (is_numeric($settings['jumpNumber']))?$settings['jumpNumber']:$this->jumpNumber; $this->firstString = (!empty($settings['firstString']))?$settings['firstString']:$this->firstString; } } /* * Class destructor */ public function __destruct() { } /* * Name: generate() * Retrieves Pagination Link HTML * * @access PUBLIC * @param ARRAY $array - MultiDimensional Array containing all results * @param NUMERIC $currentPage - Optional number of the current page of results * * @return ARRAY returns array containing results within range of pagination * */ public function generate($array, $currentPage = null) { // Get the length of the array // Get the number of pages // Check if current Page was passed or if we need to get it automatically $this->page = $_GET[$this->pageKey]; // check for page in URI string $this->page = $currentPage; // get default value start page passed in function } else { $this->page = 1; // default to page 1 if not found } // Make sure page is in range otherwise default to last page if ($this->page > $this->pages) { $this->page = $this->pages; } // Calculate the starting point // Return the part of the array we have requested } /* * Name: links() * Retrieves Pagination Link HTML * * @access PUBLIC * @return STRING returns string containing pagination navigation * */ public function links() { // Initiate the arrays and variables used in this function $queryURL = ''; // ReCreate the queries passed along the url to add to the page numbering string foreach ($_GET as $key => $value) { if ($key != $this->pageKey) { $queryURL .= '&' . $key . '=' . $value; } } } // If we have more then one pages if (($this->pages) > 1) { // Assign the 'previous page' link into the array if we are not on the first page if ($this->page != 1) { if ($this->showFirstAndLast) { $leftLinks[] = ' <a class="paginate firstPage" title="First Page" href="?' . $this->pageKey . '=1' . $queryURL . '">' . $this->firstString . '</a> '; } $leftLinks[] = ' <a class="paginate prevPage" title="Previous Page" href="?' . $this->pageKey . '=' . ($this->page - 1) . $queryURL . '">' . $this->prevString . '</a> '; } // Assign all the page numbers & links to the array for ($i = 1; $i < ($this->pages + 1); $i++) { if ($this->page == $i) { $links[] = ' <span class="paginate pageSelected">' . $i . '</span> '; // If we are on the same page as the current item } else { $links[] = ' <a class="paginate" href="?' . $this->pageKey . '=' . $i . $queryURL . '">' . $i . '</a> '; // add the link to the array } } // Assign the 'next page' if we are not on the last page if ($this->page < $this->pages) { $rightLinks[] = ' <a class="paginate nextPage" title="Next Page" href="?' . $this->pageKey . '='.($this->page + 1).$queryURL.'">' . $this->nextString . '</a> '; if ($this->showFirstAndLast) { $rightLinks[] = ' <a class="paginate lastPage" title="Last Page" href="?' . $this->pageKey . '='.($this->pages).$queryURL.'">' . $this->lastString . '</a> '; } } // Get all of the page links for display if($this->pages > $this->numOfPagesToShow) { // enforce maximum amount of page links to show if ($firstPages < 1) { $firstPages = 0; } // ensure it isn't a negative num if (count($linksOut) < $this->numOfPagesToShow) { // if we are near the end, make sure we are showing proper num of pages to show $firstPages = $this->pages - $this->numOfPagesToShow; } if ($this->page > $this->jumpNumber) { $leftLinks[] = ' <a class="paginate pageJump" title="Go Back ' . $this->jumpNumber . ' Pages" href="?' . $this->pageKey . '='.($this->page - $this->jumpNumber).$queryURL.'">' . $this->jumpString . '</a> '; } } if ($this->page < ($this->pages - $this->jumpNumber)) { array_unshift($rightLinks,' <a class="paginate pageJump" title="Jump Ahead ' . $this->jumpNumber .' Pages" href="?' . $this->pageKey . '='.($this->page + $this->jumpNumber).$queryURL.'">' . $this->jumpString . '</a> '); } } } else { $linksOut = $links; } // Push the arrays into a string and spit it back out } return; } // end of Links Function } // end of Class ?>
URL: http://kretzmann.net/paginate/