PHP PHP的数组分页类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP PHP的数组分页类相关的知识,希望对你有一定的参考价值。

<?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 (Shane@Kretzmann.net)
  *     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 = '' ) {
      if (is_array($settings)){ // if settings array is passed, update class variables
        $this->perPage              = (is_numeric($settings['perPage']))?$settings['perPage']:$this->perPage;
        $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->pageKey              = (!empty($settings['pageKey']))?$settings['pageKey']:$this->pageKey;
        $this->firstString          = (!empty($settings['firstString']))?$settings['firstString']:$this->firstString;
        $this->lastString           = (!empty($settings['lastString']))?$settings['lastString']:$this->lastString;
        $this->nextString           = (!empty($settings['nextString']))?$settings['nextString']:$this->nextString;
        $this->prevString           = (!empty($settings['prevString']))?$settings['prevString']:$this->prevString;
        $this->jumpString         = (!empty($settings['jumpString']))?$settings['jumpString']:$this->jumpString;
      }
    }

	/*
	 * 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
      $this->length = count($array);
  
      // Get the number of pages
      $this->pages = ceil($this->length / $this->perPage);
    
      // Check if current Page was passed or if we need to get it automatically
      if (isset($_GET[$this->pageKey]) && $_GET[$this->pageKey] > 0) {
        $this->page = $_GET[$this->pageKey]; // check for page in URI string  
      } else if (is_int($currentPage)) {
          $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 
      $this->start  = ceil(($this->page - 1) * $this->perPage);
  
      // Return the part of the array we have requested
      return array_slice($array, $this->start, $this->perPage);
    }

	/*
	 *  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
      $leftLinks = array();
      $links = array();
      $rightLinks = array();
      $queryURL = '';
      
      // ReCreate the queries passed along the url to add to the page numbering string
      if (count($_GET)) {
        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
          $firstPages = floor($this->page - ($this->numOfPagesToShow/2))-1;       
          
          if ($firstPages < 1) { $firstPages = 0; } // ensure it isn't a negative num 
  
          $linksOut = array_slice($links, $firstPages, $this->numOfPagesToShow+1);        
  
          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;
            $linksOut = array_slice($links, $firstPages, $this->numOfPagesToShow+1);
          }
          
          if (!empty($leftLinks) && $this->showJumpLinks) {
            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 (!empty($rightLinks) && $this->showJumpLinks) {
            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 implode(' ', $leftLinks).implode('', $linksOut).implode(' ', $rightLinks);
      }
      return;
    } // end of Links Function
  } // end of Class
?>

以上是关于PHP PHP的数组分页类的主要内容,如果未能解决你的问题,请参考以下文章

php 分页类 兄弟连的分页类无法获取到url的参数和值。

PHP碎码——分页类

PHP通用分页类

PHP分页类及用法

php分页类

PHP简单漂亮的分页类