PHP General Page class page.php[imitation Google pagination]_php instance

Source: Internet
Author: User
Tags ereg prev
page.php
Copy Code code as follows:

<?php

/**
* * General PHP Paging class. (Imitation Google style)
* * Just provide a total number of records and two parameters per page display. (Detailed use instructions are attached.)
* * without specifying a URL, the link is generated by the program. Convenient for retrieving results paging.
* * form is submitted with Get method to ensure that URL parameters are not lost when operations such as queries are deleted.
**/

Class pager{
IE Address Bar address
var $url;
Record total number of bars
var $countall;
Total pages
var $page;
Pagination Digital Link
var $thestr;
First page, Previous link
var $backstr;
Last page, Next link
var $nextstr;
Current page number
var $pg;
Show number of records per page
var $countlist;
Flip Style
var $style;
constructor, which executes the function automatically when instantiating the class
function Pager ($countall, $countlist, $style = "page") {
Number of records and display of each page can not be a team, the page after the rest plus 1
$this->countall = $countall;
$this->countlist = $countlist;
$this->style= $style;
if ($this->countall% $this->countlist!=0) {
$this->page=sprintf ("%d", $this->countall/$this->countlist) +1;
}else{
$this->page= $this->countall/$this->countlist;
}

$this->pg=$_get["PG"];
Ensure that PG is not specified to start at page 1th
if (!ereg ("^[1-9][0-9]*$", $this->pg) | | empty ($this-&GT;PG)) {
$this->pg=1;
}
Page number is out of maximum range, maximum value
if ($this->pg> $this->page) {
$this->pg= $this->page;
}
Gets the current URL. See the bottom of the function of the actual implementation of the entity
$this->url = Pager::geturl ();
Replace the wrong formatted page number with the correct page number
if (Isset ($_get["PG")) && $_get["PG"]!= $this-&GT;PG) {
$this->url=str_replace ("pg=". $_get["PG"], "? pg= $this->pg", $this->url);
$this->url=str_replace ("&pg=". $_get["PG"], "&pg= $this->pg", $this->url);
}
Generates pagination in digital form 12345.
if ($this->page<=10) {
For ($i =1 $i < $this->page+1; $i + +) {
$this->thestr= $this->thestr. PAGER::MAKEPG ($i, $this-&GT;PG);
}
}else{
if ($this->pg<=5) {
For ($i =1 $i <10; $i + +) {
$this->thestr= $this->thestr. PAGER::MAKEPG ($i, $this-&GT;PG);
}
}else{
if (6+ $this->pg<= $this->page) {
for ($i = $this->pg-4; $i < $this->pg+6; $i + +) {
$this->thestr= $this->thestr. PAGER::MAKEPG ($i, $this-&GT;PG);
}
}else{
for ($i = $this->pg-4; $i < $this->page+1; $i + +) {
$this->thestr= $this->thestr. PAGER::MAKEPG ($i, $this-&GT;PG);
}

}
}
}
Generate a text link on the next page of the page
$this->backstr = pager::gotoback ($this->pg);
$this->nextstr = Pager::gotonext ($this->pg, $this->page);
Echo ("Total" $this->countall. "Strip, per page". $this->countlist. " , $this->page. " Page ". $this->backstr. $this->thestr. $this->nextstr);
}
Auxiliary functions for generating digital paging
function Makepg ($i, $PG) {
if ($i = = $PG) {
Return "<font class= '". $this->style. "' > ". $i." </font> ";
}else{
Return "<a href=". PAGER::REPLACEPG ($this->url,5, $i). "class=". $this->style. "' ><u> ". $i." </u></a> ";
}
}
A function that generates information such as the previous page
function Gotoback ($PG) {
if ($pg -1>0) {
return $this->gotoback= "<a href=". PAGER::REPLACEPG ($this->url,3,0). "class=". $this->style. "' > Home </a> <a href= ". PAGER::REPLACEPG ($this->url,2,0). "class=". $this->style. "' > Prev </a> ";
}else{
return $this->gotoback= "<span class=". $this->style. "' > Home prev </span> ";
}
}
A function that generates information such as the next page
function GoToNext ($PG, $page) {
if ($pg < $page) {
Return "<a href=". PAGER::REPLACEPG ($this->url,1,0). "class=". $this->style. "' > next page </a> <a href= ". PAGER::REPLACEPG ($this->url,4,0). "class=". $this->style. "' > Last </a> ";
}else{
Return "<span class= '". $this->style. "' > Next Last </span> ";
}
}
Handles the $pg method in the URL for automatic generation of pg=x
function Replacepg ($url, $flag, $i) {
if ($flag = = 1) {
$temp _PG = $this->pg;
Return Str_replace ("pg=". $temp _pg, "pg=". $this->pg+1), $url);
}else if ($flag = = 2) {
$temp _PG = $this->pg;
Return Str_replace ("pg=". $temp _pg, "pg=". $this->pg-1), $url);
}else if ($flag = = 3) {
$temp _PG = $this->pg;
Return Str_replace ("pg=". $temp _pg, "pg=1", $url);
}else if ($flag = = 4) {
$temp _PG = $this->pg;
Return Str_replace ("pg=". $temp _pg, "pg=". $this->page, $url);
}else if ($flag = = 5) {
$temp _PG = $this->pg;
Return Str_replace ("pg=". $temp _pg, "pg=". $i, $url);
}else{
return $url;
}
}
How to get the current URL
function GetUrl () {
$url = "http://" $_server["http_host"];
if (Isset ($_server["Request_uri"])) {
$url. =$_server["Request_uri"];
}else{
$url. =$_server["Php_self"];
if (!empty ($_server["query_string"])) {
$url. = "?". $_server["Query_string"];
}
}
Add pg=x words to the current URL
if (!ereg () (pg=| pg=|pg=| pg=) ", $url)) {
if (!strpos ($url, "?")) {
$url = $url. "? Pg=1 ";
}else{
$url = $url. " &pg=1 ";
}
}
return $url;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.