<?php
/**
 * Pagination class
 * Making pagination of records easy(ier)
 * @author Michael Peacock
 * @url www.michaelpeacock.co.uk
 * @url www.peacockcarter.co.uk
 */
class Pagination {
    
    
/**
     * MySQLi Connection
     */
    
private $db;
    
    
/**
     * The query we will be paginating
     */
    
private $query "";
    
    
/**
     * The processed query which will be executed
     */
    
private $executedQuery "";
    
    
/**
     * The results from the query
     */
    
private $results;
    
    
/**
     * The maximum number of results to display per page
     */
    
private $limit 25;
    
    
/**
     * The results offset - i.e. page we are on (-1)
     */
    
private $offset 0;
    
    
/**
     * The number of rows there were in the query passed
     */
    
private $numRows;
    
    
/**
     * The number of rows on the current page (main use if on last page, may not have as many as limit on the page)
     */
    
private $numRowsPage;
    
    
/**
     * Number of pages of results there are
     */
    
private $numPages;
    
    
/**
     * Is this the first page of results?
     */
    
private $isFirst;
    
    
/**
     * Is this the last page of results?
     */
    
private $isLast;
    
    
/**
     * The current page we are on
     */
    
private $currentPage;
    
    
/**
     * Our constructor
     * @param mysqli $mysqli_link
     * @return void
     */
    
function __construct$mysqli_link 
    {
        
$this->db $mysqli_link;
    }
    
    
/**
     * Set the query to be paginated
     * @param String $sql the query
     * @return void
     */
    
public function setQuery$sql )
    {
        
$this->query $sql;
    }
    
    
/**
     * Set the limit of how many results should be displayed per page
     * @param int $limit the limit
     * @return void
     */
    
public function setLimit$limit )
    {
        
$this->limit $limit;    
    }
    
    
/**
     * Set the offset - i.e. if offset is 1, then we show the next page of results
     * @param int $offset the offset
     * @return void
     */
    
public function setOffset$offset )
    {
        
$this->offset $offset;
    }
    
    
/**
     * Process the query, and set the paginated properties
     * @return bool
     */
    
public function generatePagination()
    {
        
$temp_query $this->query;
        
        
// how many results?
        
$q mysqli_query$this->db$temp_query );
        
$nums mysqli_num_rows$q );
        
$this->numRows $nums;
        
        
// limit!
        
$limit " LIMIT ";
        
$limit .= ( $this->offset $this->limit ) . ", " $this->limit;
        
$temp_query $temp_query $limit;
        
$this->executedQuery $temp_query;
        
        
$q mysqli_query$this->db$temp_query );
        while( 
$row mysqli_fetch_array$qMYSQLI_ASSOC ) )
        {
            
$this->results[] = $row;
        }
        
        
// be nice...do some calculations
        
        // num pages
        
$this->numPages ceil($this->numRows $this->limit);
        
        
// is first
        
$this->isFirst = ( $this->offset == ) ? true false;
        
        
// is last
        
        
$this->isLast = ( ( $this->offset ) == $this->numPages ) ? true false;
        
        
// current page
        
$this->currentPage = ( $this->numPages == ) ? $this->offset +1;
        
$this->numRowsPage mysqli_num_rows$q );
        if( 
$this->numRowsPage == )
        {
            return 
false;
        }
        else
        {
            return 
true;
        }
        
    }
    
    
/**
     * Get the result set
     * @return array
     */
    
public function getResults()
    {
        return 
$this->results;
    }
    
    
/**
     * Get the number of pages of results there are
     * @return int
     */
    
public function getNumPages()
    {
        return 
$this->numPages;
    }
    
    
/**
     * Is this page the first page of results?
     * @return bool
     */
    
public function isFirst()
    {
        return 
$this->isFirst;
    }
    
    
/**
     * Is this page the last page of results?
     * @return bool
     */
    
public function isLast()
    {
        return 
$this->isLast;
    }
    
    
/**
     * Get the current page within the paginated results we are viewing
     * @return int
     */
    
public function getCurrentPage()
    {
        return 
$this->currentPage;
    }
    
    
/**
     * Get the number of rows there are on the current page
     * @return int
     */
    
public function getNumRowsPage()
    {
        return 
$this->numRowsPage;        
    }
}
?>