SRD75

pagination.php

Jan 15th, 2022
840
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class pagination
  4. {
  5.  
  6.   /**
  7.    * Properties array
  8.    * @var array
  9.    * @access private
  10.    */
  11.   private $_properties = array();
  12.  
  13.   /**
  14.    * Default configurations
  15.    * @var array
  16.    * @access public
  17.    */
  18.   public $_defaults = array(
  19.     'page' => 1,
  20.     'perPage' => 20
  21.   );
  22.  
  23.   /**
  24.    * Constructor
  25.    *
  26.    * @param array $array   Array of results to be paginated
  27.    * @param int   $curPage The current page interger that should used
  28.    * @param int   $perPage The amount of items that should be show per page
  29.    * @return void
  30.    * @access public
  31.    */
  32.   public function __construct($array, $curPage = null, $perPage = null)
  33.   {
  34.     $this->array   = $array;
  35.     $this->curPage = ($curPage == null ? $this->defaults['page']    : $curPage);
  36.     $this->perPage = ($perPage == null ? $this->defaults['perPage'] : $perPage);
  37.   }
  38.  
  39.   /**
  40.    * Global setter
  41.    *
  42.    * Utilises the properties array
  43.    *
  44.    * @param string $name  The name of the property to set
  45.    * @param string $value The value that the property is assigned
  46.    * @return void
  47.    * @access public
  48.    */
  49.   public function __set($name, $value)
  50.   {
  51.     $this->_properties[$name] = $value;
  52.   }
  53.  
  54.   /**
  55.    * Global getter
  56.    *
  57.    * Takes a param from the properties array if it exists
  58.    *
  59.    * @param string $name The name of the property to get
  60.    * @return mixed Either the property from the internal
  61.    * properties array or false if isn't set
  62.    * @access public
  63.    */
  64.   public function __get($name)
  65.   {
  66.     if (array_key_exists($name, $this->_properties)) {
  67.       return $this->_properties[$name];
  68.     }
  69.     return false;
  70.   }
  71.  
  72.   /**
  73.    * Set the show first and last configuration
  74.    *
  75.    * This will enable the "<< first" and "last >>" style
  76.    * links
  77.    *
  78.    * @param boolean $showFirstAndLast True to show, false to hide.
  79.    * @return void
  80.    * @access public
  81.    */
  82.   public function setShowFirstAndLast($showFirstAndLast)
  83.   {
  84.       $this->_showFirstAndLast = $showFirstAndLast;
  85.   }
  86.  
  87.   /**
  88.    * Set the main seperator character
  89.    *
  90.    * By default this will implode an empty string
  91.    *
  92.    * @param string $mainSeperator The seperator between the page numbers
  93.    * @return void
  94.    * @access public
  95.    */
  96.   public function setMainSeperator($mainSeperator)
  97.   {
  98.     $this->mainSeperator = $mainSeperator;
  99.   }
  100.  
  101.   /**
  102.    * Get the result portion from the provided array
  103.    *
  104.    * @return array Reduced array with correct calculated offset
  105.    * @access public
  106.    */
  107.   public function getResults()
  108.   {
  109.     // Assign the page variable
  110.     if (empty($this->curPage) !== false) {
  111.       $this->page = $this->curPage; // using the get method
  112.     } else {
  113.       $this->page = 1; // if we don't have a page number then assume we are on the first page
  114.     }
  115.  
  116.     // Take the length of the array
  117.     $this->length = count($this->array);
  118.  
  119.     // Get the number of pages
  120.     $this->pages = ceil($this->length / $this->perPage);
  121.  
  122.     // Calculate the starting point
  123.     $this->start = ceil(($this->page - 1) * $this->perPage);
  124.  
  125.     // return the portion of results
  126.     return array_slice($this->array, $this->start, $this->perPage);
  127.   }
  128.  
  129.   /**
  130.    * Get the html links for the generated page offset
  131.    *
  132.    * @param array $params A list of parameters (probably get/post) to
  133.    * pass around with each request
  134.    * @return mixed  Return description (if any) ...
  135.    * @access public
  136.    */
  137.   public function getLinks($params = array())
  138.   {
  139.     // Initiate the links array
  140.     $plinks = array();
  141.     $links = array();
  142.     $slinks = array();
  143.  
  144.     // Concatenate the get variables to add to the page numbering string
  145.     $queryUrl = '';
  146.     if (!empty($params) === true) {
  147.       unset($params['page']);
  148.       $queryUrl = '&amp;'.http_build_query($params);
  149.     }
  150.  
  151.     // If we have more then one pages
  152.     if (($this->pages) > 1) {
  153.       // Assign the 'previous page' link into the array if we are not on the first page
  154.       if ($this->page != 1) {
  155.         if ($this->_showFirstAndLast) {
  156.           $plinks[] = ' <a href="?page=1'.$queryUrl.'">&laquo;&laquo; First </a> ';
  157.         }
  158.         $plinks[] = ' <a href="?page='.($this->page - 1).$queryUrl.'">&laquo; Prev</a> ';
  159.       }
  160.  
  161.       // Assign all the page numbers & links to the array
  162.       for ($j = 1; $j < ($this->pages + 1); $j++) {
  163.         if ($this->page == $j) {
  164.           $links[] = ' <a class="selected">'.$j.'</a> '; // If we are on the same page as the current item
  165.         } else {
  166.           $links[] = ' <a href="?page='.$j.$queryUrl.'">'.$j.'</a> '; // add the link to the array
  167.         }
  168.       }
  169.  
  170.       // Assign the 'next page' if we are not on the last page
  171.       if ($this->page < $this->pages) {
  172.         $slinks[] = ' <a href="?page='.($this->page + 1).$queryUrl.'"> Next &raquo; </a> ';
  173.         if ($this->_showFirstAndLast) {
  174.           $slinks[] = ' <a href="?page='.($this->pages).$queryUrl.'"> Last &raquo;&raquo; </a> ';
  175.         }
  176.       }
  177.  
  178.       // Push the array into a string using any some glue
  179.       return implode(' ', $plinks).implode($this->mainSeperator, $links).implode(' ', $slinks);
  180.     }
  181.     return;
  182.   }
  183. }
  184.  
RAW Paste Data