PHP paging code. The PHP paging code provides a piece of code today. this is a php paging code: Create a paging class classPage {private $ _ all_num; total number of data private $ _ page_size; PHP paging code per page
Today we provide you with a piece of code, which is a piece of php paging code:
// Create a paging class
Class Page {
Private $ _ all_num; // total number of data
Private $ _ page_size; // several data entries per page
Private $ _ page; // The current page
Private $ _ all_page; // total page number
Private $ _ url; // page address
Private $ _ limit; // database query statement
Private $ _ sign; // address symbol
// Interceptor
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;
}
// Obtain the 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;
}
}
// Obtain the 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;
}
// Display the page
Public function numPage (){
$ _ Num = '';
// Front
For ($ I = 1; $ I <$ this-> _ page; $ I ++ ){
If ($ I <1) continue;
$ _ Num. = "". $ I ."";
}
// Yourself
$ _ Num. = "". $ this-> _ page ."";
// After
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 code to the text editor to make it more comfortable! The following describes the specific steps:
1. create a paging class: class Page {};
2. get the number of data to be displayed and record it in the Page class. here the number of data is obtained through the data, which is not detailed! Set the number of data entries displayed on each page!
3. get the current page address!
This code roughly means that when your address is followed? When you click the page, use & page =; when your address is not followed by a suffix or only? Page =, so click the page to use? Page =
4. obtain the current page number
The page number must be an integer. Therefore, you can use the ceil function. pay attention to the malicious modification of the address!
4. create paging elements. here we just create a simple digital paging, which is more complex and provides better user experience. you can expand it on your own!
5. set the number of queries in the database, that is, LIMIT. for example, if I have 10 data records and set to display 5 data records on each page
When the user clicks the first page, the LIMIT 0, 5
When the user clicks the second page, the LIMIT 5
6. call this code:
$ _ Page = new Page (10, 5 );
$ _ Page-> page ();
The 10 above is the total number of data, and 5 is the number of data that you want to display on each page. $ _ page-> page () is the page, just echo it out, note that you have to call $ _ limit in the Page class and put it in the database query statement, so that the basic php Page will come out!
This article is original to Wang yelou's personal blog. if you want to reprint it, please indicate the source: Wang yelou's personal blog www.ly89.cn.
Today, Parse provides you with a piece of code, which is a php paging code: // creates a paging class Page {private $ _ all_num; // The total number of data private $ _ page_size; // every page...