PHP Pagination Class
PHP Pagination Class
<?php
$sql = "
SELECT *
FROM `media`
WHERE active='1'
ORDER BY `media_date` DESC
";
$pagination = new pagination();
$pagination->adjacents = 3;
$pagination->limit = 2;
$pagination->countSQL = $sql;
$pagination->targetpage = $_SERVER['PHP_SELF'];;
$pagination->page = $_GET['page'];
$query = null;
$pagination->sartQuery = $urlQuery;
$pagination->endQuery = null;
$result = $db->query($sql);
$pagination->totalPages = $result->num_rows;
$start = $pagination->one();
$sql .= " LIMIT $start, 2 ";
.
.
.
.
.
<?= $pagination->two(); ?>
/*------------------------- class -------------------------------*/
<?php
class pagination
{
public $adjacents;
public $limit;
public $countSQL;
public $targetpage;
public $totalPages;
public $start;
public $page;
public $sartQuery;
public $endQuery;
function one()
{
if(!$this->totalPages)
{
//$result = $db->query($this->countSQL);
//$this->totalPages = $result->num_rows;
}
if($this->page)
$start = ($this->page - 1) * $this->limit;
else
$start = 0;
return $start;
}
function two()
{
$startaddon = null;
$endaddon = null;
if(!empty($this->sartQuery)) $startaddon = $this->sartQuery;
if(!empty($this->endQuery))
$endaddon = $this->endQuery;
if ($this->page == 0) $this->page = 1;
$prev = $this->page - 1;
$next = $this->page + 1;
$lastpage = ceil($this->totalPages/$this->limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class='pagination'>";
if ($this->page > 1)
$pagination.= "<a class='page' href='$this->targ
etpage?$startaddon&page=$prev&pagination=1$endaddon'>« previous</a>";
else
$pagination.= "<span class='disabled page'>&laqu
o; previous</span>";
if ($lastpage < 7 + ($this->adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $count
er++)
{
if ($counter == $this->page)
$pagination.= "<span class='curr
ent page active'>$counter</span>";
else
$pagination.= "<a class='page' h
ref='$this->targetpage?$startaddon&page=$counter&pagination=1$endaddon'>$counter
</a>";
}
}
elseif($lastpage > 5 + ($this->adjacents * 2))
{
if($this->page < 1 + ($this->adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($this
->adjacents * 2); $counter++)
{
if ($counter == $this->page)
$pagination.= "<span cla
ss='current page'>$counter</span>";
else