Beautiful paging method implemented by php

Source: Internet
Author: User

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;
}

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.