PHP anti-Google pagination effect Code _php Tutorial

Source: Internet
Author: User
function Getpagerange ($currentPage, $totalPages, $displaySize = 10) {
if ($totalPages <= 0 | | $displaySize <= 0) {
return Array ();
} elseif ($displaySize > $totalPages) {
$startPage = 1;
$endPage = $totalPages;
} else {
if ($currentPage% $displaySize = = = 0) {
$startPage = $currentPage-$displaySize + 1;
} else {
while (($currentPage% $displaySize)) {
-$currentPage;
}
$startPage = $currentPage + 1;
}
if ($startPage <= 0) {
$startPage = 1;
}
$endPage = $startPage + $displaySize-1;
if ($endPage > $totalPages) {
$endPage = $totalPages;
$startPage = $endPage-$displaySize + 1;
}
}
Return range ($startPage, $endPage);
} functionGetpagerange accepts three parameters, the current page $currentpage, the total number of pages $totalpages, and the paging interval length $displaysize, the default is 10. Based on these three parameters, the function Getpagerange generates an appropriate paging interval containing the $currentpage. First of all, we need to exclude illegal parameter values, for the total number of pages or the interval length of less than 0 should be checked.

Then, we examine the idea of static partitioning. As mentioned earlier, given the length of the paging interval, the total number of pages can be divided by the length to get the interval. At the same time, we can analyze that not all ranges contain the same number of pages, and in extreme cases, the total number of pages is less than the given page-turn interval length, then the result of division or cut will always have only one interval. Fortunately, this does not interfere with our core algorithms, but we still need to focus on the robustness of the code. So, let's consider the extreme, the use of algorithms SolveBefore the core problem, quickly capture only one page of the interval.

Next, we can look at the intrinsic properties of the interval. Each dynamic cutting interval has a start page and a tail end; Since the intervals are sequential, we will always get the first (first) interval and the last (trailing) interval after the static partition, if the end-to-back interval is coincident, the total number of pages is less than the given paging interval length. However, the key problem that the algorithm needs to solve is how to find the start page and the end of the interval, and once the two elements are identified, you can use the PHP built-in range function to generate all the page numbers within the range.

The algorithm compares the current page $currentpage and the paging interval length $displaysize to determine the position of the current page in the interval, and then deduces the offset between the interval start page and the last page. In order to be perfect, we also have to consider the problem of boundary overflow, which is very simple, just to determine whether the start page and the end is between 1 and the total number of pages.

At this point, the analysis of the code has been completed. Let's take a look at the algorithm TimeEfficiency, popularly said, the basic operation of the algorithm is to find the model, the execution time of the algorithm depends on the difference between $currentpage and $displaysize, the greater the difference, the more the number of modulus, the longer the execution time, a linear structure. The actual execution results are accomplished in an instant.

Next, let's combine the Google search results page above and use the Getpagerange function to generate a page-turn interval and enter the required parameters:
Print_r (Implode (', ', Getpagerange (18, 27, 20)); The result is:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 you will find this is completely different from the Google search results page. Yes, because Google's flip Zone forces the current page SetIn the middle of the position! Well, don't lose heart, we can still use the Getpagerange function to get the result that matches it, just decompose the problem:
Print_r (Implode (', ', Array_merge (Getpagerange (+), Getpagerange (27, 27, 10))), the result is:
8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27


http://www.bkjia.com/PHPjc/445001.html www.bkjia.com true http://www.bkjia.com/PHPjc/445001.html techarticle function Getpagerange ($currentPage, $totalPages, $displaySize = ten) {if ($totalPages = 0 | | $displaySize = 0) {retur n Array (); } elseif ($displaySize $totalPages) {$startPage = ...

  • 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.