Skip to content

Instantly share code, notes, and snippets.

@Zvax
Created August 2, 2017 01:38
Show Gist options
  • Save Zvax/6c09de4ea95b4cc0587861c9d2dd6c82 to your computer and use it in GitHub Desktop.
Save Zvax/6c09de4ea95b4cc0587861c9d2dd6c82 to your computer and use it in GitHub Desktop.
overhaul of old paginator class
<?php
/**
* PHPSense Pagination Class
*
* PHP tutorials and scripts
*
* @package PHPSense
* @author Jatinder Singh Thind
* @copyright Copyright (c) 2006, Jatinder Singh Thind
* @link http://www.phpsense.com
*/
/**
* Class Paginator
*
* 2017-01-01 oop overhaul bu zvax after a question on stack overflow
* no copyright infringements are intended, but considering the ancientness of this code
* I figure this is fair game
*/
// ------------------------------------------------------------------------
class Paginator
{
private $php_self;
private $rows_per_page = 10; //Number of records to display per page
private $total_rows = 0; //Total number of rows returned by the query
private $links_per_page = 5; //Number of links to display per page
private $append = ""; //Paremeters to append to pagination links
private $sql = "";
private $debug;
private $conn = false;
private $page = 1;
private $max_pages = 0;
private $offset = 0;
/**
* Constructor
*
* @param mysqli $connection Mysql connection link
* @param string $sql SQL query to paginate. Example : SELECT * FROM users
* @param integer $rows_per_page Number of records to display per page. Defaults to 10
* @param integer $links_per_page Number of links to display per page. Defaults to 5
* @param string $append Parameters to be appended to pagination links
* @param boolean $debug
*/
public function __construct(
mysqli $connection,
$sql,
$rows_per_page = 10,
$links_per_page = 5,
$append = "",
$debug = false
)
{
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = (int)$rows_per_page;
$this->debug = $debug;
if (intval($links_per_page) > 0)
{
$this->links_per_page = (int)$links_per_page;
}
else
{
$this->links_per_page = 5;
}
$this->append = $append;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
if (isset($_GET['page']))
{
$this->page = intval($_GET['page']);
}
}
/**
* Executes the SQL query and initializes internal variables
*
* @access public
* @return resource
*/
public function paginate()
{
// Check for valid mysql connection
// no longer needed as we typehint the connection object,
// ensuring we receive a connection or an error will be thrown
// Find total number of rows
$all_rs = $this->conn->query($this->sql);
// the connection should be built with the throw exceptions param
// so no need to verify manually, error reporting will take care of it
$this->total_rows = $all_rs->num_rows;
//Return FALSE if no rows found
if ($this->total_rows == 0)
{
if ($this->debug)
{
echo "Query returned zero rows.";
}
return FALSE;
}
//Max number of pages
$this->max_pages = ceil($this->total_rows / $this->rows_per_page);
if ($this->links_per_page > $this->max_pages)
{
$this->links_per_page = $this->max_pages;
}
//Check the page value just in case someone is trying to input an aribitrary value
if ($this->page > $this->max_pages || $this->page <= 0)
{
$this->page = 1;
}
//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page - 1);
//Fetch the required result set
$rs = $this->conn->query($this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}");
if (!$rs)
{
if ($this->debug)
{
echo "Pagination query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
}
return false;
}
return $rs->fetch_all(MYSQLI_ASSOC);
}
/**
* Display the link to the first page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'First'
* @return string
*/
public function renderFirst($tag = 'First')
{
if ($this->total_rows == 0)
{
return FALSE;
}
if ($this->page == 1)
{
return '"previous-off">' . $tag;
}
else
{
return '"next"><a href="' . $this->php_self . '?page=1&amp;' . $this->append . '">' . $tag . '</a> ';
}
}
/**
* Display the link to the last page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'Last'
* @return string
*/
public function renderLast($tag = 'Last')
{
if ($this->total_rows == 0)
{
return FALSE;
}
if ($this->page == $this->max_pages)
{
return '"previous-off">' . $tag;
}
else
{
return '"next"><a href="' . $this->php_self . '?page=' . $this->max_pages . '&amp;' . $this->append . '">' . $tag . '</a>';
}
}
/**
* Display the next link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '>>'
* @return string
*/
public function renderNext($tag = '&gt;&gt;')
{
if ($this->total_rows == 0)
{
return FALSE;
}
if ($this->page < $this->max_pages)
{
return '"next"><a href="' . $this->php_self . '?page=' . ($this->page + 1) . '&amp;' . $this->append . '">' . $tag . '</a>';
}
else
{
return '"next-off">' . $tag;
}
}
/**
* Display the previous link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '<<'
* @return string
*/
public function renderPrev($tag = '&lt;&lt;')
{
if ($this->total_rows == 0)
{
return FALSE;
}
if ($this->page > 1)
{
return ' "next"><a href="' . $this->php_self . '?page=' . ($this->page - 1) . '&amp;' . $this->append . '">' . $tag . '</a>';
}
else
{
return '"previous-off">' . $tag;
}
}
/**
* Display the page links
*
* @access public
* @return string
*/
public function renderNav($prefix = '<span class="page_link">', $suffix = '</span>')
{
if ($this->total_rows == 0)
{
return FALSE;
}
$batch = ceil($this->page / $this->links_per_page);
$end = $batch * $this->links_per_page;
if ($end == $this->page)
{
//$end = $end + $this->links_per_page - 1;
//$end = $end + ceil($this->links_per_page/2);
}
if ($end > $this->max_pages)
{
$end = $this->max_pages;
}
$start = $end - $this->links_per_page + 1;
$links = '';
for ($i = $start; $i <= $end; $i++)
{
if ($i == $this->page)
{
$links .= $prefix . ' class="active">' . "$i" . $suffix;
}
else
{
$links .= ' ' . $prefix . '><a href="' . $this->php_self . '?page=' . $i . '&amp;' . $this->append . '">' . $i . '</a>' . $suffix . ' ';
}
}
return $links;
}
/**
* Display full pagination navigation
*
* @access public
* @return string
*/
public function renderFullNav()
{
return $this->renderFirst() . '&nbsp;' . $this->renderPrev() . '&nbsp;' . $this->renderNav(
) . '&nbsp;' . $this->renderNext() . '&nbsp;' . $this->renderLast();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment