Only you need to make and connect database connection and call from your class to getPaginationList() mthod in this class. It will return set of pagination data. $link_per_page must be a even number.
class PaginationQueries {
private $sql; //sql quary for retrieve data from database
private $db;
private $php_self;
private $min;
private $max_list;
private $link_per_page;
public function __construct() {
$this->db = new DBConnection(); //database connection
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']); //self url in the address bar
}
/**
* pagination list details
*
* @param string $sql sql query
* @param int $max_list maximum number of documents per page
* @param int $min current page
*
* @return array containing link for every page
*/
function getPaginationList($sql, $min, $max_list, $link_per_page) {
$this->sql = $sql;
$this->min = $min; //pagination starting from this number
$this->max_list = $max_list;//number of links in this page
$this->link_per_page = $link_per_page;
return $this->moveFirst() . $this->movePreByBlock() . $this->paginationListDecrease() . '<span class="pagination_list now"><a> ' . ($this->min + 1) . ' </a></span>' . $this->paginationListIncrease() . $this->moveNextByBlock() . $this->moveLast();
}
/**
* @return decreacing link list from current page
*/
function paginationListDecrease() {
$link = '';
$decreace_link = $this->link_per_page / 2;
for ($count = $this->min - $decreace_link; $count < $this->min; $count++) {
if ($count >= 0) {
$parameters = explode('=', $this->php_self);
$link.="<span class='pagination_list' id='$count'><a href='" . $parameters[0] . "=$count'> " . ($count + 1) . " </a></span>";
}
}
return $link;
}
/**
* @return increasing link list from current page
*/
function paginationListIncrease() {
$numberOfRows = $this->db->query($this->sql, array());
$increace_link = $this->link_per_page / 2;
$number = $numberOfRows->fetch();
$numberOfPages = floor($number[0] / $this->max_list);
$link = '';
for ($count = $this->min + 1; $count <= $this->min + $increace_link; $count++) {
if ($count <= $numberOfPages) {
$parameters = explode('=', $this->php_self);
$link.="<span class='pagination_list' id='$count'><a href='" . $parameters[0] . "=$count'> " . ($count + 1) . " </a></span>";
}
}
return $link;
}
/**
* @return link to move first page
*/
function moveFirst() {
$parameters = explode('=', $this->php_self);
return "<span class='pagination_list' id='0'><a href='" . $parameters[0] . "=0'> First </a></span>";
}
/**
* @return link to move last page
*/
function moveLast() {
$numberOfRows = $this->db->query($this->sql, array());
$number = $numberOfRows->fetch();
$numberOfPages = floor($number[0] / $this->max_list);
$parameters = explode('=', $this->php_self);
return "<span class='pagination_list' id='$numberOfPages'><a href='" . $parameters[0] . "=$numberOfPages'> Last </a></span>";
}
/**
*
* @param string $sql sql query for populate data(optional parameter)
* @param int $min current page(optional parameter)
* @param int $max_list maximum number of documents per page
*
* @return link to move next page
*/
function moveNext($sql = '', $min = 0, $max_list = 0) {
if ($sql == '') {
$sql = $this->sql;
$min = $this->min;
$max_list = $this->max_list;
}
$numberOfRows = $this->db->query($sql, array());
$number = $numberOfRows->fetch();
$numberOfPages = floor($number[0] / $max_list);
$parameters = explode('=', $this->php_self);
$next_page = $parameters[1] + 1;
if ($next_page >= $numberOfPages) {
return "<span class='pagination_list'><a href='#'> » </a></span>";
} else {
return "<span class='pagination_list'id='$next_page'><a href='" . $parameters[0] . "=$next_page'> » </a></span>";
}
}
/**
* @return link to move previous page
*/
function movePrevious() {
$parameters = explode('=', $this->php_self);
$previous_page = $parameters[1] - 1;
if ($previous_page <= 0) {
return "<span class='pagination_list'><a href='#'> « </a></span>";
}
return "<span class='pagination_list' id='$previous_page'><a href='" . $parameters[0] . "=$previous_page'> « </a></span>";
}
/**
* @return link to move previous page
*/
function moveNextByBlock() {
$numberOfRows = $this->db->query($this->sql, array());
$number = $numberOfRows->fetch();
$numberOfPages = floor($number[0] / $this->max_list);
$parameters = explode('=', $this->php_self);
$next_page = $parameters[1] + ($this->link_per_page);
if ($next_page >= $numberOfPages) {
return "<span class='pagination_list' id='$numberOfPages'><a href='" . $parameters[0] . "=$numberOfPages'> » </a></span>";
} else {
return "<span class='pagination_list' id='$next_page'><a href='" . $parameters[0] . "=$next_page'> » </a></span>";
}
}
/**
* @return link to move previous page
*/
function movePreByBlock() {
$parameters = explode('=', $this->php_self);
$previous_page = $parameters[1] - ($this->link_per_page);
if ($previous_page <= 0) {
return "<span class='pagination_list' id='0'><a href='" . $parameters[0] . "=0'> « </a></span>";
}
return "<span class='pagination_list' id='$previous_page'><a href='" . $parameters[0] . "=$previous_page'> « </a></span>";
}
}