Location: PHPKode > scripts > Paginator Iterator > paginator-iterator/pi_lib/PaginatorIterator.php
<?php
/**
 * Paginator Iterator Class
 *
 * @author      Anderson A. Meggiolaro <hide@address.com>
 * @license     LGPL
 *
 * Simple usage:
 -------------------------------------------------------------------------------------------------------
 |	include('PaginatorIterator.php');
 |	PaginatorIterator::setConnection(new PDO('mysql:dbname=j15;host=127.0.0.1', 'root', '123456'));
 |
 |	try {
 |
 |		$paginator=new PaginatorIterator;
 |		$paginator->ds="select * from jos_menu limit :LIMIT offset :OFFSET";
 |		$paginator->numrows="select count(*) from jos_menu";
 |		$paginator->pageno=$_GET['pageno'];
 |		$paginator->rowsperpage=6;
 |		return $paginator->paginate();
 |	} 
 |	catch(Exception $e) {
 |		throw $e;
 |	}
 -------------------------------------------------------------------------------------------------------
 *
 */
class PaginatorIterator
{
	/**
	*	Page number
	*/
	public $pageno=1;
	
	/**
	*	Number of records per page
	*/
	public $rowsperpage=10;
	
	/**
	*	Number of links per page 
	*	First Prev (1 2 3 4 5 6 7 8 9 10) Next Last
	*/
	public $numlinks=10;
	
	/**
	*	SQL statement or Array
	*	The datasource
	*/
	public $ds;
	
	/**
	*	SQL or number
	*	Number of total lines of whole pagination
	*/
	public $numrows;

	/**
	*	Database connection
	*/
	protected static $conn;

	/**
	*	Plugin to handle the database queries
	*/
	public static $handlerFunc='pdo';
	
	public static function setHandlerFunc($value)
	{
		self::$handlerFunc=$value;
	}

	public static function getHandlerFunc()
	{
		return self::$handlerFunc;
	}

	public static function setConnection($conn)
	{
		self::$conn=$conn;
	}

	public static function &getConnection()
	{
		return self::$conn;
	}

	public function paginate()
	{
		require_once('plugins/'.self::getHandlerFunc().'.php');

		if(is_numeric($this->numrows))
			$numrows=$this->numrows;
		else
		{
			try	{
				$numrows=call_user_func_array('pi_'.self::getHandlerFunc().'_numrows', array(self::$conn, $this->numrows, $this));
			}
			catch(Exception $e) {
				throw $e;
			}
		}
		
		$lastPage=ceil($numrows/$this->rowsperpage);
		
		if(!$this->pageno)
			$this->pageno=1;

		if($this->pageno<1)
			$this->pageno=1;

		if($this->pageno>$lastPage)
			$this->pageno=$lastPage;

		$totalPaginas=intval($numrows/$this->rowsperpage);
		if(($numrows%$this->rowsperpage) != 0)
			$totalPaginas++;
			
		if($totalPaginas<1)
			$totalPaginas=1;
		
		if($totalPaginas>$lastPage)
			$totalPaginas=$lastPage;
			
		$proximaPagina=$this->pageno +1;
		if($proximaPagina>$totalPaginas)
			$proximaPagina=$totalPaginas;

		$paginaAnterior=$this->pageno -1;
		if($paginaAnterior<1)
			$paginaAnterior=1;

		$primeiraPagina=1;
		$ultimaPagina=$totalPaginas;

		$paginaAtual=$this->pageno;
		if(!$paginaAtual)
			$paginaAtual=1;

		$primeiroRegistro=((($paginaAtual * $this->rowsperpage) - $this->rowsperpage) +1);
		if($primeiroRegistro<1)
			$primeiroRegistro=1;
			
		$ultimoRegistro=$primeiroRegistro + $this->rowsperpage -1;
		if($ultimoRegistro > $numrows)
			$ultimoRegistro = $numrows;
		
		if(!$ultimoRegistro)
			$ultimoRegistro=$primeiroRegistro;
		
		if(is_array($this->ds))
			$dataSource=$this->ds;
		else
		{
			try	{
				$offset=($this->pageno*$this->rowsperpage)-$this->rowsperpage;
				$limit=$this->rowsperpage;
				$dataSource=call_user_func_array('pi_'.self::getHandlerFunc().'_ds', array(self::$conn, $this->ds, $offset, $limit, $this));
			}
			catch(Exception $e) {
				throw $e;
			}
		}

		$start=$paginaAtual;

		$end=$start + $this->numlinks;
		if($end>$totalPaginas)
			$end=$totalPaginas;
			
		if($end<1)
			$end=1;

		if(($start + $this->numlinks) >= $end)
			$start = $end - $this->numlinks;

		if($start<1)
			$start=1;

		for($i=$start; $i<=$end; $i++)
			$pages[]=$i;

		$rec=array
		(
			"ds" 		=> $dataSource,
			"pages" 	=> $pages,
			"first"		=> 1,
			"next"		=> $proximaPagina,
			"prev"		=> $paginaAnterior,
			"last"		=> $ultimaPagina,
			"numrows" 	=> $numrows,
			"pageno"	=> $paginaAtual,
			"numpages"	=> $totalPaginas,
			"rowsperpage"=> $this->rowsperpage,
			"is_first_page"=>($paginaAtual<=1),
			"is_last_page"=>($paginaAtual==$ultimaPagina)
		);

		return $rec;
	}
}

?>
Return current item: Paginator Iterator