Return to Snippet

Revision: 56131
at March 12, 2012 05:09 by uberdragon


Updated Code
<?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 = "&laquo;&laquo; First";
    var $lastString = "Last &raquo;&raquo;";
    var $nextString = "Next &raquo;";
    var $prevString = "&laquo; 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
?>

Revision: 56130
at March 12, 2012 04:35 by uberdragon


Updated Code
<?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 $showElipseJumpLinks = true; // Set true if you want to show jump links 
    var $elipseJump = 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 = "&laquo;&laquo; First";
    var $lastString = "Last &raquo;&raquo;";
    var $nextString = "Next &raquo;";
    var $prevString = "&laquo; Prev";
    var $elipseString = "...";
    

	/*
	 *  Class constructor
	 */
  	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->showElipseJumpLinks  = (isset($settings['showElipseJumpLinks']))?$settings['showElipseJumpLinks']:$this->showElipseJumpLinks; 
        $this->elipseJump           = (is_numeric($settings['elipseJump']))?$settings['elipseJump']:$this->elipseJump;
        $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->elipseString         = (!empty($settings['elipseString']))?$settings['elipseString']:$this->elipseString;
      }
    }

	/*
	 * 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->showElipseJumpLinks) {
            if ($this->page > $this->elipseJump) {
              $leftLinks[] = ' <a class="paginate pageJump" title="Go Back ' . $this->elipseJump . ' Pages" href="?' . $this->pageKey . '='.($this->page - $this->elipseJump).$queryURL.'">' . $this->elipseString . '</a> ';
            }
          }
          if (!empty($rightLinks) && $this->showElipseJumpLinks) {
            if ($this->page < ($this->pages - $this->elipseJump)) {
              array_unshift($rightLinks,' <a class="paginate pageJump" title="Jump Ahead ' . $this->elipseJump .' Pages" href="?' . $this->pageKey . '='.($this->page + $this->elipseJump).$queryURL.'">' . $this->elipseString . '</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
?>

Revision: 56129
at March 12, 2012 02:51 by uberdragon


Updated Code
<?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])
  *            
  \************************************************************/

  class pagination {
    var $page; // Current Page
    var $perPage = 10; // Items on each page, defaulted to 10
    var $numOfPagesToShow = 10; // Number of pagination links to show on page.
    var $showFirstAndLast = true; // if you would like the first and last page options.
    var $showElipseJumpLinks = true; // if you want to show jump links 
    var $elipseJump = 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 = "&laquo;&laquo; First";
    var $lastString = "Last &raquo;&raquo;";
    var $nextString = "Next &raquo;";
    var $prevString = "&laquo; Prev";
    var $elipseString = "...";
    

	/*
	 *  Class constructor
	 */
  	public function __construct( $settings = '' ) {
      if (is_array($settings)){
        $this->perPage              = $settings['perPage'];
        $this->numOfPagesToShow     = $settings['numOfPagesToShow'];
        $this->showFirstAndLast     = $settings['showFirstAndLast'];
        $this->showElipseJumpLinks  = $settings['showElipseJumpLinks'];
        $this->elipseJump           = $settings['elipseJump'];
        $this->pageKey              = $settings['pageKey'];
        $this->firstString          = $settings['firstString'];
        $this->lastString           = $settings['lastString'];
        $this->nextString           = $settings['nextString'];
        $this->prevString           = $settings['prevString'];
        $this->elipseString         = $settings['elipseString'];
      }
    }

	/*
	 * Class destructor
	 */
  	public function __destruct() {
  	    
  	}

    
	/*
	 *  Name: generate()
	 *  Retrieves Pagination Link HTML
	 * 
	 *  @access PUBLIC
   *  @param  ARRAY   $array - MultiDimensional Array containing all results
   *  @param  NUMERIC $perPage - The amount of results to return per page
   *  @param  NUMERIC $numOfPages - Maximum number of pages to show in pagination links
   *  @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, $perPage = 10, $numOfPages = 10, $currentPage = null) {
      // Assign the items per page variable
      if (!empty($perPage)) {
        $this->perPage = $perPage;
      }
      
      // assign Max number of Page links to show
      if (is_int($numOfPages)) {
        $this->numOfPagesToShow = $numOfPages;
      }   
  
      // 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->showElipseJumpLinks) {
            if ($this->page > $this->elipseJump) {
              $leftLinks[] = ' <a class="paginate elipse" title="Go Back ' . $this->elipseJump . ' Pages" href="?' . $this->pageKey . '='.($this->page - $this->elipseJump).$queryURL.'">' . $this->elipseString . '</a> ';
            }
          }
          if (!empty($rightLinks) && $this->showElipseJumpLinks) {
            if ($this->page < ($this->pages - $this->elipseJump)) {
              array_unshift($rightLinks,' <a class="paginate elipse" title="Jump Ahead ' . $this->elipseJump .' Pages" href="?' . $this->pageKey . '='.($this->page + $this->elipseJump).$queryURL.'">' . $this->elipseString . '</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
?>

Revision: 56128
at March 11, 2012 11:46 by uberdragon


Updated Code
<?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])
  *            
  \************************************************************/

  class pagination {
    var $page; // Current Page
    var $perPage; // Items on each page, defaulted to 10
    var $numOfPagesToShow; // Number of pagination links to show on page.
    var $showFirstAndLast = true; // if you would like the first and last page options.
    var $showElipseJumpLinks = true; // if you want to show jump links 
    var $elipseJump = 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 = "&laquo;&laquo;First";
    var $lastString = "Last&raquo;&raquo;";
    var $nextString = "Next&raquo;";
    var $prevString = "&laquo;Prev";
    var $elipseString = "...";
    
    
    function generate($array, $perPage = 10, $numOfPages = 10, $currentPage = null) {
      // Assign the items per page variable
      if (!empty($perPage)) {
        $this->perPage = $perPage;
      }
      
      // assign Max number of Page links to show
      if (is_int($numOfPages)) {
        $this->numOfPagesToShow = $numOfPages;
      }   
  
      // 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);
    }
    
    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[] = ' <a class="paginate selected">' . $i . '</a> '; // 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->showElipseJumpLinks) {
            if ($this->page > $this->elipseJump) {
              $leftLinks[] = ' <a class="paginate elipse" title="Go Back ' . $this->elipseJump . ' Pages" href="?' . $this->pageKey . '='.($this->page - $this->elipseJump).$queryURL.'">' . $this->elipseString . '</a> ';
            }
          }
          if (!empty($rightLinks) && $this->showElipseJumpLinks) {
            if ($this->page < ($this->pages - $this->elipseJump)) {
              array_unshift($rightLinks,' <a class="paginate elipse" title="Jump Ahead ' . $this->elipseJump .' Pages" href="?' . $this->pageKey . '='.($this->page + $this->elipseJump).$queryURL.'">' . $this->elipseString . '</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
?>

Revision: 56127
at March 11, 2012 11:32 by uberdragon


Updated Code
<?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])
  *            
  \************************************************************/

  class pagination {
    var $page; // Current Page
    var $perPage; // Items on each page, defaulted to 10
    var $numOfPagesToShow; // Number of pagination links to show on page.
    var $showFirstAndLast = true; // if you would like the first and last page options.
    var $showElipseJumpLinks = true; // if you want to show jump links (jumps twice the numOfPages set)
    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 = "&laquo;&laquo;First";
    var $lastString = "Last&raquo;&raquo;";
    var $nextString = "Next&raquo;";
    var $prevString = "&laquo;Prev";
    var $elipseString = "...";
    
    
    function generate($array, $perPage = 10, $numOfPages = 10, $currentPage = null) {
      // Assign the items per page variable
      if (!empty($perPage)) {
        $this->perPage = $perPage;
      }
      
      // assign Max number of Page links to show
      if (is_int($numOfPages)) {
        $this->numOfPagesToShow = $numOfPages;
      }   
  
      // 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);
    }
    
    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" href="?' . $this->pageKey . '=1' . $queryURL . '">' . $this->firstString . '</a> ';
          }
          $leftLinks[] = ' <a class="paginate prevPage" 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[] = ' <a class="paginate selected">' . $i . '</a> '; // 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" href="?' . $this->pageKey . '='.($this->page + 1).$queryURL.'">' . $this->nextString . '</a> ';
          if ($this->showFirstAndLast) {
            $rightLinks[] = ' <a class="paginate lastPage" 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->showElipseJumpLinks) {
            if ($this->page > $this->numOfPagesToShow*2) {
              $leftLinks[] = ' <a class="paginate elipse" href="?' . $this->pageKey . '='.($this->page - $this->numOfPagesToShow*2).$queryURL.'">' . $this->elipseString . '</a> ';
            }
          }
          if (!empty($rightLinks) && $this->showElipseJumpLinks) {
            if ($this->page < ($this->pages - $this->numOfPagesToShow*2)) {
              array_unshift($rightLinks,' <a class="paginate elipse" href="?' . $this->pageKey . '='.($this->page + $this->numOfPagesToShow*2).$queryURL.'">' . $this->elipseString . '</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
?>

Revision: 56126
at March 11, 2012 11:27 by uberdragon


Initial Code
<?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])
  *            
  \************************************************************/

  class pagination {
    var $page; // Current Page
    var $perPage; // Items on each page, defaulted to 10
    var $numOfPagesToShow; // Number of pagination links to show on page.
    var $showFirstAndLast = true; // if you would like the first and last page options.
    var $showElipseJumpLinks = true; // if you want to show jump links (jumps twice the numOfPages set)
    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 = "&laquo;&laquo;First";
    var $lastString = "Last&raquo;&raquo;";
    var $nextString = "Next&raquo;";
    var $prevString = "&laquo;Prev";
    var $elipseString = "...";
    
    
    function generate($array, $perPage = 10, $numOfPages = 10, $currentPage = null) {
      // Assign the items per page variable
      if (!empty($perPage)) {
        $this->perPage = $perPage;
      }
      
      // assign Max number of Page links to show
      if (is_int($numOfPages)) {
        $this->numOfPagesToShow = $numOfPages;
      }   
  
      // 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);
    }
    
    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" href="?' . $this->pageKey . '=1' . $queryURL . '">' . $this->firstString . '</a> ';
          }
          $leftLinks[] = ' <a class="paginate prevPage" 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[] = ' <a class="paginate selected">' . $i . '</a> '; // 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" href="?' . $this->pageKey . '='.($this->page + 1).$queryURL.'">' . $this->nextString . '</a> ';
          if ($this->showFirstAndLast) {
            $rightLinks[] = ' <a class="paginate lastPage" 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->showElipseJumpLinks) {
            if ($this->page > $this->numOfPagesToShow*2) {
              $leftLinks[] = ' <a class="paginate elipse" href="?' . $this->pageKey . '='.($this->page - $this->numOfPagesToShow*2).$queryURL.'">' . $this->elipseString . '</a> ';
            }
          }
          if (!empty($rightLinks) && $this->showElipseJumpLinks) {
            if ($this->page < ($this->pages - $this->numOfPagesToShow*2)) {
              array_unshift($rightLinks,' <a class="paginate elipse" href="?' . $this->pageKey . '='.($this->page + $this->numOfPagesToShow*2).$queryURL.'">' . $this->elipseString . '</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
?>

Initial URL
http://kretzmann.net/paginate/

Initial Description
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.

Initial Title
Array Pagination Class for PHP

Initial Tags

                                

Initial Language
PHP