-
Notifications
You must be signed in to change notification settings - Fork 0
Alternate Pagination class
Category:Core | Category:Core::Community | Category:Core::Pagination
[h3]An Alternate CI Pagination Library[/h3] 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.
[h3]The library init code[/h3] This is the library init code
[b]application/init/init_altpage.php[/b] [code]<?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';
?>[/code]
[h3]The Library[/h3] This is the lib itself.
[b]application/libraries/altpage.php[/b] [code]<?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->segment_array(); if ($this->page < 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;
}
}
?>[/code]
[h3]Example Controller[/h3] Here's the simple controller
[b]application/controllers/testpagination.php[/b] [code]<?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())); } }
?>[/code]
[h3]Example View[/h3]
Here's the example view in [url=http://www.rushessay.com]custom essays[/url] 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.
[b]application/views/pgtest.php[/b]
[code]<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'].'">« First</a> '; } if ($pagination['prev']!=''){ echo ' <a href="'.$pagination['prev'].'"><</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'].'">></a> ';
}
if ($pagination['last']!=''){
echo ' <a href="'.$pagination['last'].'">Last »</a>';
}
?>
</div>
<?php endif; ?> </body> </html>[/code]