0% found this document useful (0 votes)
91 views

PHP Pagination Class

The document defines a PHP pagination class that: 1. Handles pagination for SQL queries by calculating the start record and total pages based on the page number, limit, and total row count. 2. Generates HTML pagination links and indicators for navigating between pages by generating previous/next links and page number links based on the current page and adjacent page ranges. 3. Returns the generated HTML pagination links when the pagination class's two() method is called.

Uploaded by

Gegham Movses
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

PHP Pagination Class

The document defines a PHP pagination class that: 1. Handles pagination for SQL queries by calculating the start record and total pages based on the page number, limit, and total row count. 2. Generates HTML pagination links and indicators for navigating between pages by generating previous/next links and page number links based on the current page and adjacent page ranges. 3. Returns the generated HTML pagination links when the pagination class's two() method is called.

Uploaded by

Gegham Movses
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

/* CALLING 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'>&laquo; 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

$pagination.= "<a class=


'page' href='$this->targetpage?$startaddon&page=$counter&pagination=1$endaddon'>
$counter</a>";
}
$pagination.= "...";
$pagination.= "<a class='page' href='$th
is->targetpage?$startaddon&page=$lpm1&pagination=1$endaddon'>$lpm1</a>";
$pagination.= "<a class='page' href='$th
is->targetpage?$startaddon&page=$lastpage&pagination=1$endaddon'>$lastpage</a>";
;
}
elseif($lastpage - ($this->adjacents * 2) > $thi
s->page && $this->page > ($this->adjacents * 2))
{
$pagination.= "<a class='page' href='$th
is->targetpage?$startaddon&page=1&pagination=1$endaddon'>1</a>";
$pagination.= "<a class='page' href='$th
is->targetpage?$startaddon&page=2&pagination=1$endaddon'>2</a>";
$pagination.= "...";
for ($counter = $this->page - $this->adj
acents; $counter <= $this->page + $this->adjacents; $counter++)
{
if ($counter == $this->page)
$pagination.= "<span cla
ss='current page'>$counter</span>";
else
$pagination.= "<a class=
'page' href='$this->targetpage?$startaddon&page=$counter&pagination=1$endaddon'>
$counter</a>";
}
$pagination.= "...";
$pagination.= "<a class='page' href='$th
is->targetpage?$startaddon&page=$lpm1&pagination=1$endaddon'>$lpm1</a>";
$pagination.= "<a class='page' href='$th
is->targetpage?$startaddon&page=$lastpage&pagination=1$endaddon'>$lastpage</a>";
}
else
{
$pagination.= "<a class='page' href='$th
is->targetpage?$startaddon&page=1&pagination=1$endaddon'>1</a>";
$pagination.= "<a class='page' href='$th
is->targetpage?$startaddon&page=2&pagination=1$endaddon'>2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($this>adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $this->page)
$pagination.= "<span cla
ss='current page'>$counter</span>";
else

$pagination.= "<a class=


'page' href='$this->targetpage?$startaddon&page=$counter&pagination=1$endaddon'>
$counter</a>";
}
}
}
if ($this->page < $counter - 1)
$pagination.= "<a class='page' href='$this->targ
etpage?$startaddon&page=$next&pagination=1$endaddon'>next &raquo;</a>";
else
$pagination.= "<span class='disabled page'>next
&raquo;</span>";
$pagination.= "</div>";
return $pagination;
}
}
}
?>

You might also like