I have write one pagination class using PHP. This is useful for you reference.
<?php
class Paging {
private $total_rows;
private $total_pages;
private $rows_per_page;
private $pre_sign; //the previous page sign
private $next_sign; //the next page sign
private $first_sign; //the first page sign
private $last_sign; //the last page sign
private $seperater; //the page number seperater
private $current_page;
private $start_page;
private $num_per_page; //how many page number to show
/**
* Construct function.
* @param int $total_rows the total rows number.
* @param int $rows_per_page how many row to display per page.
*/
public function __construct($total_rows, $rows_per_page, $num_page = 10) {
$this->total_rows = $total_rows;
$this->total_pages = ceil($total_rows / $rows_per_page);
$this->rows_per_page = $rows_per_page;
$this->current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$this->num_per_page = $num_page;
$this->pre_sign = '<';
$this->next_sign = '>';
$this->first_sign = '<<';
$this->last_sign = '>>';
$this->seperater = '|';
}
/**
* Show paging info.
*/
public function showPaging() {
if ($this->total_rows > $this->rows_per_page) {
$this->start_page = $this->getStartPage();
$end_page = $this->start_page + $this->num_per_page - 1;
if ($end_page > $this->total_pages) {//adjust the end page, start page
$end_page = $this->total_pages;
$this->start_page = $end_page - $this->num_per_page + 1;
}
$pages = $this->addLinkInfo($this->start_page, $end_page);
$paging = implode($this->seperater, $pages);
}
else {
$paging = '';
}
return $paging;
}
/**
* Gets start page number.
* @return int $start_page the start page number.
*/
private function getStartPage() {
$start = NULL;
$start = (5 < $this->current_page) ? ($this->current_page - 5) : 1;
return ($this->total_pages > $start) ? $start : ($this->total_pages - $this->num_per_page + 1);
}
/**
* Sets the pre page sign.
*/
public function setPreSign($pre_sign) {
$this->pre_sign = $pre_sign;
}
/**
* Sets the next page sign.
*/
public function setNextSign($next_sign) {
$this->next_sign = $next_sign;
}
/**
* Sets the first page sign.
*/
public function setFirstSign($first_sign) {
$this->first_sign = $first_sign;
}
/**
* Sets the last page sign.
*/
public function setLastSign($last_sign) {
$this->last_sign = $last_sign;
}
/**
* Sets the page seperator sign.
*/
public function setSeperator($seperator) {
$this->seperater = $seperator;
}
/**
* Adds page link info.
* @param int $start_page the start page number.
* @param int $end_page the end page number.
*/
private function addLinkInfo($start_page, $end_page) {
$page_with_link = NULL;
$paging = array();
if (1 != $start_page) {
$paging[] = $this->addPageLink($this->first_sign, 'first'); //add first page link
}
if (1 < $this->current_page) {
$paging[] = $this->addPageLink($this->current_page - 1, 'previous'); //add previous page link
}
for ($i = $start_page; $i <= $end_page; ++$i) {
if ($i != $this->current_page) {
$page_with_link = $this->addPageLink($i);
}
else {
$page_with_link = $i;
}
$paging[] = $page_with_link;
}
if ($this->current_page < $this->total_pages) {
$paging[] = $this->addPageLink($this->current_page + 1, 'next'); //add next page link
if ($this->total_pages > $end_page) { //need add the last page link
$paging[] = $this->addPageLink($this->last_sign, 'last');
}
}

return $paging;
}
/**
* Adds link info to page $page_num.
* @param int $page_num
* @return string page number with link info.
*/
private function addPageLink($page_num, $add_sign = '') {
$current_file = basename($_SERVER['PHP_SELF']);
if ('first' == $add_sign) {
$query_string = $this->getQueryString(1);
$link = <<<END
<a href="$current_file?$query_string">$page_num</a>
END;
}
else if ('last' == $add_sign) {
$query_string = $this->getQueryString($this->total_pages);
$link = <<<END
<a href="$current_file?$query_string">$page_num</a>
END;
}
else if ('previous' == $add_sign){
if (0 >= $page_num) {
$page_num = 1;
}
$query_string = $this->getQueryString($page_num);
$link = <<<END
<a href="$current_file?$query_string">$this->pre_sign</a>
END;
}
else if ('next' == $add_sign) {
if ($page_num > $this->total_pages) {
$page_num = $this->total_pages;
}
$query_string = $this->getQueryString($page_num);
$link = <<<END
<a href="$current_file?$query_string">$this->next_sign</a>
END;
}
else {
$query_string = $this->getQueryString($page_num);
$link = <<<END
<a href="$current_file?$query_string">$page_num</a>
END;
}
return $link;
}
/**
* Gets the query string from the URL.
* @param int $page set the current page number.
* @return string new query string.
*/
private function getQueryString($page) {
$query_string = $_SERVER['QUERY_STRING'];
$query_array = NULL;
parse_str($query_string, $query_array);
$query_array['page'] = $page; //set page num
return http_build_query($query_array);
}
}
$page = new Paging(1000, 10);
$page->setFirstSign('first');//set the first page sign, default is '<<'
$page->setPreSign('pre'));//set the pre page sign, default is '<'
$page->setNextSign('next'));//set the next page sign, default is '>'
$page->setLastSign('last'));//set the last page sign, default is '>>'
$page->setSeperator(']['));//set the first page sign, default is '|'
echo '[' . $page->showPaging() . ']';
?>

