PHP Generic paging class. This code is used for paging. a slight modification can be used for various programs. For usage instructions, see my article. Page. php
The code is as follows:
/**
** 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. the link is generated by the program. 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 "style." '> ". $ I ."";
} Else {
Return "url, 5, $ I)." class = '". $ this-> style."'>
". $ I ."";
}
}
// Function for generating information such as the previous page
Function gotoback ($ pg ){
If ($ PG-1> 0 ){
Return $ this-> gotoback = "url, 3, 0 ). "class = '". $ this-> style. "'> homepage url, 2, 0 ). "class = '". $ this-> style. "'> Previous Page ";
} Else {
Return $ this-> gotoback = "style." '> homepage previous page ";
}
}
// Function for generating information such as the next page
Function gotonext ($ pg, $ page ){
If ($ pg <$ page ){
Return "url, 1, 0 ). "class = '". $ this-> style. "'> Next Page url, 4, 0 ). "class = '". $ this-> style. "'> last page ";
} Else {
Return "style." '> last page of the next page ";
}
}
// 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 = | pG = | Pg =)", $ url )){
If (! Strpos ($ url ,"? ")){
$ Url = $ url ."? Pg = 1 ";
} Else {
$ Url = $ url. "& pg = 1 ";
}
}
Return $ url;
}
}
?>
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.