The paginator class basically takes an array of items and then
paginates it and returns an array with the current page range. You can
then create a table or do whatever you want with the current array set.
You can set the items per page and use the pagination control method to
create a pager.
The Picasa Gallery also uses this
class to paginate the albums and photos retrieved through RSS feeds.
Post # |
Title |
Content |
Date |
11 | test_11 | test_11 | November 23, 2024 09:13:04 am |
12 | test_12 | test_12 | November 23, 2024 09:13:04 am |
13 | test_13 | test_13 | November 23, 2024 09:13:04 am |
14 | test_14 | test_14 | November 23, 2024 09:13:04 am |
Paginator.php
<?php
/**
* Paginator class, takes an array and creates a pagination object
* that can be use to go to different pages of that array.
*
* @author Herbert Balagtas
* @copyright 2010 Herbert Balagtas
*
*/
class Paginator {
/**
* Items to paginate
* @var Array
*/
protected $content_array = array ();
/**
* Items per page
* @var $ipp Int
*/
protected $ipp = 15;
/**
* The current page
* @var $totalitems Int
*/
public $cp = 1;
/**
* @var $totalitems Int
*/
public $totalitems;
/**
* Total page
* @var $totalpage Int
*/
public $totalpage;
/**
* Current array set
* @var Array
*/
public $current_set;
/**
* first item in range
* @var Int
*/
public $fi;
/**
* last item in range
* @var Int
*/
public $li;
protected $offset;
/**
* Constructor for Paginator class
* @param constructor $content_array
*/
public function __construct($content_array) {
$this->content_array = $content_array;
$this->totalitems = count ( $content_array );
$this->getTotalPage ();
}
/**
* get property value
* @param String $property
*/
function __get($property) {
return $this->$property;
}
/**
* Items per page
* @param Int $ipp
*/
public function setIPP($ipp) {
$this->ipp = $ipp;
$this->getTotalPage ();
}
public function getItemRange() {
if ($this->cp > $this->totalpage) {
$this->cp = 1;
}
$this->li = ($this->cp * $this->ipp);
$offset = 0;
if ($this->li > $this->totalitems) {
$this->offset = $this->li - $this->totalitems;
$this->li = $this->totalitems;
}
$this->fi = $this->li - $this->ipp + $this->offset;
}
/**
* Get items for the current page
*/
public function getItems() {
$this->getItemRange();
for($x = $this->fi; $x < $this->li; $x ++) {
$this->current_set [] = $this->content_array [$x];
}
return $this->current_set;
}
/**
* Set the current page
* @param Int $pg
* @return the current page of the paginator
*/
public function CurrentPage($pg) {
if (! empty ( $pg ) && $pg <= $this->totalpage) {
$this->cp = $pg;
} elseif ($pg > $this->totalpage) {
$this->cp = 1;
}
return $this->cp;
}
/**
* Get the total page based on the number of items passe to the class
*/
public function getTotalPage() {
$this->totalpage = ceil ( $this->totalitems / $this->ipp );
return $this->totalpage;
}
/**
* Returns the next set of items
*/
public function getNext() {
$this->np = $this->cp + 1;
if ($this->np > $this->totalpage)
$this->np = 1;
return $this->np;
}
/**
* Returns the previous set of items
*/
public function getPrevious() {
$this->pp = $this->cp - 1;
if ($this->pp < 1)
$this->pp = $this->totalpage;
return $this->pp;
}
/**
* Returns the first set of items
*/
public function getFirst() {
return 1;
}
/**
* Returns the last set of items
*/
public function getLast() {
return $this->totalpage;
}
/**
* Function to generate a quick paginator control, you can however create your own
*
* @param GET POST variables $params
* @param DIV Id for the controller $divid
*/
public function paginationControl($params, $divid) {
$this->getItemRange();
unset ( $params ['pg'] );
$urlparams = (! empty ( $params ) ? '&' . http_build_query ( $params ) : '');
if ($this->getTotalPage () > 10) {
if ($this->cp > 5) {
$startpage = $this->cp - 5;
} else {
$startpage = 1;
}
$endpage = $this->cp + 5;
if ($endpage > $this->getTotalPage ()) {
$endpage = $this->getTotalPage ();
} elseif ($endpage < 10 && $this->getTotalPage () > 10) {
$endpage = 10;
}
} else {
$startpage = 1;
$endpage = $this->getTotalPage ();
}
for($x = $startpage; $x <= $endpage; $x ++) {
if ($x != $this->cp) {
$pages [] = '<a href="?pg=' . $x . $urlparams . '">' . $x . '</a>';
} else {
$pages [] = $x;
}
}
// Items in range
$paginationControl = '';
$paginationControl .= '<div id="' . $divid . '">';
$paginationControl .= '[' . ( $this->fi + 1 ) . ' to ' . $this->li . ' of ' . $this->totalitems . '] ';
if ($this->cp > 1) {
$paginationControl .= '<a href="?pg=' . $this->getFirst () . $urlparams . '">First</a> | ';
} else {
$paginationControl .= 'First | ';
}
$paginationControl .= '<a href="?pg=' . $this->getPrevious () . $urlparams . '">Previous</a> ';
$paginationControl .= implode ( " | ", $pages );
$paginationControl .= ' | <a href="?pg=' . $this->getNext () . $urlparams . '">Next</a>';
if ($this->cp != $this->totalpage) {
$paginationControl .= ' | <a href="?pg=' . $this->getLast () . $urlparams . '">Last</a>';
} else {
$paginationControl .= ' | Last';
}
$paginationControl .= '</div>';
return $paginationControl;
}
}
PaginatorDemo.php
<?php
include('paginator.php');
for ($x = 1; $x<=88; $x++){
$posts[] = array('postno'=>$x,'title'=>'test_'.$x, 'content'=>'test_'.$x, 'date'=>date('F j, Y h:i:s a') );
}
$paginator = new Paginator($posts);
$paginator->setIPP(15);
if (!empty($_GET['pg'])){
$paginator->CurrentPage($_GET['pg']);
} else {
$paginator->CurrentPage(1);
}
$params = array_merge($_GET, $_POST);
echo $paginator->paginationControl($params, 'control1');
?>
<table style="width: 100%;" id="table1">
<tr>
<td>Post #</td>
<td>Title</td>
<td>Content</td>
<td>Date</td>
</tr>
<?php
foreach ($paginator->getItems() as $post){
echo '<tr>';
echo '<td>'.$post['postno'].'</td>';
echo '<td>'.$post['title'].'</td>';
echo '<td>'.$post['content'].'</td>';
echo '<td>'.$post['date'].'</td>';
echo '</tr>';
}
?>