Skip to content

Alternate Pagination class

Deathart edited this page Aug 27, 2015 · 24 revisions

Category:Core | Category:Core::Community | Category:Core::Pagination

An Alternate CI Pagination Library

This alternate pagination lib (Requires PHP5) uses page numbers in the URI as opposed to the offsets used by the stock class. Where the default CI class returns a html string, this class returns an array to be formatted by your own templates or views.

The library init code

This is the library init code

application/init/init_altpage.php

<?php if (!defined('BASEPATH')) exit('Direct access not permitted!');

if (! class_exists('Altpage')){
  include_once(APPPATH.'libraries/Altpage'.EXT);
}

$obj =& get_instance();
$obj->altpage = new Altpage();
$obj->ci_is_loaded[] = 'altpage';

?>

The Library

This is the lib itself.

application/libraries/altpage.php

<?php
/* CI License & stuff */
class Altpage {

  public $page = 0;
  public $per_page = 20;
  public $total = 0;
  public $uri_segment;

  private $_link_uri = '';

  public function __construct($p=array()){
    if (count($p) > 0) $this->init($p);
    log_message('debug', 'Altpage Class Initialized');
  }

  public function init($p){
    foreach($p as $k => $v){ $this->$k = $v; }
  }

  private function _setLinkURI(){
    $obj =& get_instance();
    $segs = $obj->uri-&gt;segment_array(); 
    if ($this->page &lt; 1){
      if (isset($segs[$this->uri_segment])){
        $this->page = (int) $segs[$this->uri_segment];
      } else {
        $this->page = 1;
      }
    }
    $segs[$this->uri_segment] = '%d';
    $this->_link_uri = $obj->config->item('base_url').implode('/', $segs);
  }

  private function _getLink($page){
    return (string) sprintf($this->_link_uri, $page);
  }

  public function create_links(){
    $num_pages = (int) ceil($this->total / $this->per_page);
    if ($this->total == 0 OR $this->per_page == 0 OR $num_pages < 2) return '';
    $this->_setLinkURI();
    $pg=array();
    /* Upto 2 pages either side + current page */
    $pg['pages']=array();
    $t=(bool)($this->page < 2);
    $pg['first'] =($t)? '': $this->_getLink(1);
    $pg['prev'] = ($t)? '': $this->_getLink($this->page - 1);
    if ($t===false){
      $i=2;
      while ($i>0){
        if (($this->page - $i) > 0){
          $pg['pages'][$this->page - $i] = $this->_getLink($this->page - $i);
        } 
        --$i;
      }
    }
    /* Current page should not be a clickable link! */
    $pg['pages'][$this->page]=$this->page;
    $t=(bool)($this->page < $num_pages);
    if ($t===true){
      $i=1;
      while ($i<=2){
        if (($this->page + $i) <= $num_pages){
          $pg['pages'][$this->page + $i] = $this->_getLink($this->page + $i);
        }
        ++$i;
      }
    }    
    $pg['next'] = ($t)? $this->_getLink($this->page + 1): '';
    $pg['last'] = ($t)? $this->_getLink($num_pages): '';
    return $pg;
  }
}
  
?>

Example Controller

Here's the simple controller

application/controllers/testpagination.php

<php

class Testpagination extends Controller {
  
  function index(){
    $this->load->library('altpage');
    $_p['total'] = 200; /* Total results */
    $_p['per_page'] = 20; /* Results Per Page */
    $_p['uri_segment']=2; /* Used to read and set pages */
    $this->altpage->init($_p);
    /* Pass pagination as array to the view */
    $this->load->view('pgtest', array('pagination'=>$this->altpage->create_links()));
  }
}

?>

Example View

Here's the example view in customized paper, [http://customresearchpapers.co.uk/| academic writing] and custom essays complete with example CSS, note how the current page is handled in the loop because you'll need to deal with this in your template or view.

English Translations

application/views/pgtest.php

<html>
 <head>
  <title>Alternate Pagination Class For CI</title>
  <style>
   body { background-color: #000; font-family: Tahoma, Verdana; }
   #pagination { background-color: #eee; text-align: center; 
                 margin: 2em; padding: 0.75em; border: 4px solid #bad; }
   .currentpage,
   #pagination a:link,
   #pagination a:visited { color: #000; text-decoration: none; 
                            background-color: transparent;
                            padding: 2px; border: 1px solid #000; }
   #pagination a:hover { color: #fff; background-color: #bad; }
   #pagination a:focus,
   .currentpage { background-color: #fff; border: 1px solid #bad; 
                            font-weight: bold; }   
  </style>
 </head>
<body>
<?php

if (isset($pagination)):

?>

  <div id="pagination">
    <?php 
      if ($pagination['first']!=''){
        echo '<a href="'.$pagination['first'].'">&laquo; First</a>';
      }
      if ($pagination['prev']!=''){
        echo '<a href="'.$pagination['prev'].'">&#60;</a>';
      }
      foreach ($pagination['pages'] as $page=>$link){

        /* NOTE: Current page should NOT be a clickable link */
        if ($page==$link){
          echo '<span class="currentpage">'.$page.'</span>';
        } else {
          echo '<a href="'.$link.'">'.$page.'</a>';
        }
      }
      if ($pagination['next']!=''){
        echo '<a href="'.$pagination['next'].'">&#62;</a>';
      }
      if ($pagination['last']!=''){
        echo '<a href="'.$pagination['last'].'">Last &raquo;</a>';
      }
    ?>
  </div>

<?php
endif;
?>
 </body>
</html>
Clone this wiki locally