PHP generic pagination page. php [Google pagination]

Source: Internet
Author: User
Tags ereg

Page. php CopyCode The Code is as follows: <? PHP

/**
** Generic PHP paging class. (Like Google style)
** You only need to provide two parameters: the total number of records and the number of records displayed on each page. (Detailed instructions are provided ..)
** You do not need to specify a URL.ProgramGenerate. It is convenient for querying results by page.
** The form is submitted using the get method, which ensures that URL parameters are not lost in operations such as query and deletion.
**/

Class pager {
// IE Address Bar
VaR $ URL;
// The total number of records
VaR $ countall;
// Total number of pages
VaR $ page;
// Pagination numeric link
VaR $ thestr;
// Home page and previous page
VaR $ backstr;
// Link to the last page and next page
VaR $ nextstr;
// Current page number
VaR $ PG;
// Number of records displayed per page
VaR $ countlist;
// Flip Style
VaR $ style;
// Constructor. This function is automatically executed when the class is instantiated.
Function Pager ($ countall, $ countlist, $ style = "page "){
// When the number of records is different from the number displayed on each page, add 1 after the remaining number of pages
$ 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 starts from page 1st if not specified
If (! Ereg ("^ [1-9] [0-9] * $", $ this-> PG) | empty ($ this-> PG )){
$ This-> Pg = 1;
}
// The page number exceeds the maximum value.
If ($ this-> PG >$ this-> page ){
$ This-> Pg = $ this-> page;
}
// Obtain the current URL. For specific implementation, see the function entity at the bottom.
$ This-> url = Pager: geturl ();
// Replace the page number with the correct format
If (isset ($ _ Get ["PG"]) & $ _ Get ["PG"]! = $ This-> 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 );
}
// Generate a page with numbers such as 12345.
If ($ this-> page <= 10 ){
For ($ I = 1; $ I <$ this-> page + 1; $ I ++ ){
$ This-> thestr = $ this-> thestr. Pager: makepg ($ I, $ this-> PG );
}
} Else {
If ($ this-> PG <= 5 ){
For ($ I = 1; $ I <10; $ I ++ ){
$ This-> thestr = $ this-> thestr. Pager: makepg ($ I, $ this-> 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-> PG );
}
} Else {
For ($ I = $ this-> pg-4; $ I <$ this-> page + 1; $ I ++ ){
$ This-> thestr = $ this-> thestr. Pager: makepg ($ I, $ this-> PG );
}

}
}
}
// Generate text links such as the top and bottom pages
$ This-> backstr = Pager: gotoback ($ this-> PG );
$ This-> nextstr = Pager: gotonext ($ this-> PG, $ this-> page );
// Echo ("Total ". $ this-> countall. "entries, per page ". $ this-> countlist. "entries, total ". $ this-> page. "page ". $ this-> backstr. $ this-> thestr. $ this-> nextstr );
}
// Auxiliary function 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> ";
}
}
// Function for generating 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. "'> homepage </a> <a href = ". pager: replacepg ($ this-> URL, 2, 0 ). "Class = '". $ this-> style. "'> previous page </a> ";
} Else {
Return $ this-> gotoback = "<SPAN class = '". $ this-> style. "'> Home page </span> ";
}
}
// Function for generating 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 page </a> ";
} Else {
Return "<SPAN class = '". $ this-> style. "'> last page of the next page </span> ";
}
}
// Process the $ PG method in the URL, which is used to automatically generate 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;
}
}
// Obtain 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 to the current URL
If (! Ereg ("(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.