Page number display algorithm
Copy codeThe Code is as follows:
/**
* Get the HTML content of the page
* @ Param integer $ page Current page
* @ Param integer $ total pages of pages
* @ Param string $ the number of pages at the end of the url to be appended with '& page = x'
*
* @ Return string HTML content;
*/
Public static function getPageHtml ($ page, $ pages, $ url ){
// Maximum number of pages displayed
$ _ PageNum = 5;
// If the current page is smaller than 1, the value is 1.
$ Page = $ page <1? 1: $ page;
// If the current page is greater than the total page number, it is the total page number.
$ Page = $ page> $ pages? $ Pages: $ page;
// The page size is small. The current page is the current page.
$ Pages = $ pages <$ page? $ Page: $ pages;
// Computing start page
$ _ Start = $ page-floor ($ _ pageNum/2 );
$ _ Start = $ _ start <1? 1: $ _ start;
// Computing end page
$ _ End = $ page + floor ($ _ pageNum/2 );
$ _ End =$ _ end> $ pages? $ Pages: $ _ end;
// The number of currently displayed pages is not enough. Adjust the number of pages left and right.
$ _ CurPageNum =$ _ end-$ _ start + 1;
// Adjust left
If ($ _ curPageNum <$ _ pageNum & $ _ start> 1 ){
$ _ Start = $ _ start-($ _ pageNum-$ _ curPageNum );
$ _ Start = $ _ start <1? 1: $ _ start;
$ _ CurPageNum =$ _ end-$ _ start + 1;
}
// Adjust on the right
If ($ _ curPageNum <$ _ pageNum & $ _ end <$ pages ){
$ _ End = $ _ end + ($ _ pageNum-$ _ curPageNum );
$ _ End =$ _ end> $ pages? $ Pages: $ _ end;
}
$ _ PageHtml = '<ul class = "pagination"> ';
/* If ($ _ start = 1 ){
$ _ PageHtml. = '<li> <a title = "first page"> «</a> </li> ';
} Else {
$ _ PageHtml. = '<li> <a title = "first page" href = "'. $ url. '& page = 1 "> «</a> </li> ';
}*/
If ($ page> 1 ){
$ _ PageHtml. = '<li> <a title = "Previous Page" href = "'. $ url. '& page = '. ($ page-1 ). '"> «</a> </li> ';
}
For ($ I =$ _ start; $ I <=$ _ end; $ I ++ ){
If ($ I = $ page ){
$ _ PageHtml. = '<li class = "active"> <a>'. $ I. '</a> </li> ';
} Else {
$ _ PageHtml. = '<li> <a href = "'. $ url. '& page = '. $ I. '"> '. $ I. '</a> </li> ';
}
}
/* If ($ _ end = $ pages ){
$ _ PageHtml. = '<li> <a title = "last page">» </a> </li> ';
} Else {
$ _ PageHtml. = '<li> <a title = "last page" href = "'. $ url. '& page = '. $ pages. '">» </a> </li> ';
}*/
If ($ page <$ _ end ){
$ _ PageHtml. = '<li> <a title = "Next" href = "'. $ url. '& page = '. ($ page + 1 ). '">» </a> </li> ';
}
$ _ PageHtml. = '</ul> ';
Echo $ _ pageHtml;
}