Location: PHPKode > projects > php Blogger > libs/pagination.class.php
<?php
/************************************************************\
*
*	  PHP Array Pagination Copyright 2007 - Derek Harvey
*	  www.lotsofcode.com
*
*	  This file is part of PHP Array Pagination .
*
*	  PHP Array Pagination is free software; you can redistribute it and/or modify
*	  it under the terms of the GNU General Public License as published by
*	  the Free Software Foundation; either version 2 of the License, or
*	  (at your option) any later version.
*
*	  PHP Array Pagination is distributed in the hope that it will be useful,
*	  but WITHOUT ANY WARRANTY; without even the implied warranty of
*	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
*	  GNU General Public License for more details.
*
*	  You should have received a copy of the GNU General Public License
*	  along with PHP Array Pagination ; if not, write to the Free Software
*	  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA	02111-1307	USA
*
* \************************************************************/
class pagination {
  var $page = 1;
  var $perPage = 10;
  var $showFirstAndLast = false;
  /**
   * pagination::generate()
   * 
   * @param mixed $array
   * @param integer $perPage
   * @return
   */
  function generate($array,$perPage = 10) {
    if(!empty($perPage))
      $this->perPage = $perPage;
    if(!empty($_GET['page'])) {
      $this->page = $_GET['page'];
    } else {
      $this->page = 1;
    }
    $this->length = count($array);
    $this->pages = ceil($this->length / $this->perPage);
    $this->start = ceil(($this->page - 1) * $this->perPage);
    return array_slice($array,$this->start,$this->perPage);
  }
  /**
   * pagination::links()
   * 
   * @return
   */
  function links() {
    $plinks = array();
    $links = array();
    $slinks = array();
    if(count($_GET)) {
      $queryURL = '';
      foreach($_GET as $key => $value) {
        if($key != 'page') {
          $queryURL .= '&'.$key.'='.$value;
        }
      }
    }
    if(($this->pages) > 1) {
      if($this->page != 1) {
        if($this->showFirstAndLast) {
          $plinks[] = ' <a href="?page=1'.$queryURL.'">&laquo;&laquo; [ First ] </a> ';
        }
        $plinks[] = ' <a href="?page='.($this->page - 1).$queryURL.'" rel="prev" title="prev">&laquo; [ Prev ]</a> ';
      }
      for($j = 1; $j < ($this->pages + 1); $j++) {
        if($this->page == $j) {
          $links[] = ' <a class="selected">[ '.$j.' ]</a> ';
        } else {
          @$links[] = ' <a href="?page='.$j.$queryURL.'" alt=Page: "'.$j.'" title="Page: '.$j.'">[ '.$j.' ]</a> ';
        }
      }
      if($this->page < $this->pages) {
        @$slinks[] = ' <a href="?page='.($this->page + 1).$queryURL.'" rel="next" title="next"> [ Next ] &raquo; </a> ';
        if($this->showFirstAndLast) {
          @$slinks[] = ' <a href="?page='.($this->pages).$queryURL.
            '"> [ Last ] &raquo;&raquo; </a> ';
        }
      }
      return implode(' ',$plinks).implode(@$this->implodeBy,$links).implode(' ',$slinks);
    }
    return;
  }
  /**
   * pagination::linkser()
   * 
   * @return
   */
  function linkser() {
    // Initiate the links array
    $plinks = array();
    $links = array();
    $slinks = array();
    if(count($_GET)) {
      $queryURL = '';
      foreach($_GET as $key => $value) {
        if($key != 'page') {
          $queryURL .= '&'.$key.'='.$value;
        }
      }
    }
    if(($this->pages) > 1) {
      for($j = 1; $j < ($this->pages + 1); $j++) {
      }
      @$slinks[] = $this->pages;
      return implode(' ',$plinks).implode(@$this->implodeBy,$links).implode(' ',$slinks);
    }
    return;
  }
} ?>
Return current item: php Blogger