PHP Pagination Class Code
Today is a piece of code for everyone, this is a PHP page code:
Create a paging class
Class Page {
Private $_all_num; Total number of data
Private $_page_size; Show several data per page
Private $_page; Current page
Private $_all_page; Total Page number
Private $_url; Page address
Private $_limit; Database query Statements
Private $_sign; Address symbol
Interception device
Public Function __get ($_key) {
return $this->$_key;
}
Public function __construct ($_all_num,$_page_size) {
$this->_all_num = $_all_num;
$this->_page_size = $_page_size;
$this->_all_page = ceil ($this->_all_num/$this->_page_size);
$this->_page = $this->setpage ();
$this->_url = $this->seturl ();
$this->_limit = ' limit '. ($this->_page * $this->_page_size-$this->_page_size). ', '. $this->_page_size;
}
Get current page number
Private Function Setpage () {
if (!empty ($_get[' page ')) && $_get[' page '] > 0) {
if ($_get[' page '] <= $this->_all_page) {
return ceil ($_get[' page ');
} else {
return $this->_all_page;
}
} else {
return 1;
}
}
Get address
Private Function SetUrl () {
$_url = $_server[' Request_uri '];
if (!empty ($_url)) {
$_par_url = Parse_url ($_url);
if (isset ($_par_url[' query ')) {
PARSE_STR ($_par_url[' query '],$_arr);
if (Isset ($_arr[' page ')) {
unset ($_arr[' page ');
}
if (!empty ($_arr)) {
$_http = Http_build_query ($_arr);
$_end_url = $_par_url[' path '). '?' . $_http;
$this->_sign = ' & ';
} else {
$_end_url = $_par_url[' path '];
$this->_sign = '? ';
}
} else {
$_end_url = $_par_url[' path '];
$this->_sign = '? ';
}
}
return $_end_url;
}
Show pagination
Public Function Numpage () {
$_num = ";
Front
for ($i =1; $i < $this->_page; $i +) {
if ($i <1) continue;
$_num. = "". $i. "";
}
Myself
$_num. = "". $this->_page. "";
Behind
for ($i = $this->_page+1; $i <= $this->_all_page; $i + +) {
if ($i > $this->_all_page) continue;
$_num. = "". $i. "";
}
return $_num;
}
}
?>
You can copy the above piece of code into a text editor so it will be more comfortable to see! Here are the steps:
1. Create a paging Class: Class Page {};
2, the need to display the number of data, and recorded in the Page class, where the number of data to get through the data is not detailed said! Then set the number of bars to display data per page!
3, get the current page address!
This code probably means when your address is followed, then click on the page to use &page=; when your address does not have a suffix or only page=, then click on the page to use? page=
4. Get the current page number
Here the page number must be an integer, so you can use ceil this function, but also pay attention to the user's malicious modification address!
4, do pagination element, here just do a simple digital paging, more complex user experience better, we can expand their own!
5, set the database library query number, that is, limit; For example: I now have 10 data, set to display 5 data per page, then
When the user clicks on the first page, the LIMIT 0,5
When the user clicks on the second page, the LIMIT 5,5
6, the Call of this code:
$_page = new page (10,5);
$_page->page ();
The above 10 is the total number of data, 5 is you want to display the number of data per page, $_page->page () is the page paging, directly echo out of the line, note that you have to put the page class $_limit call out into the database query statement, This basic PHP page is out of the way!
This article belongs to Wang Yip Building's personal blog original, to reprint please indicate the source: Wang Ye Lou's personal blog www.ly89.cn
http://www.bkjia.com/PHPjc/1011352.html www.bkjia.com true http://www.bkjia.com/PHPjc/1011352.html techarticle PHP Page Class code today for everyone to present a piece of code, this is a PHP page code://Create the page class page {private $_all_num;//The total number of data private $_page_size;//per page ...