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 );
} The getPageRange function accepts three parameters: the current page $ currentPage, the total number of pages $ totalPages, and the page turning interval length $ displaySize. The default value is 10. Based on the three parameters, the getPageRange function generates a paging interval that contains $ currentPage. First, we need to exclude invalid parameter values. If the total number of pages or the interval length is less than zero, we must check them.
Then, let's examine the idea of static division. As mentioned above, given the length of the paging interval, the total number of pages can be divided by the length to obtain the number of segments. At the same time, we can find that not all intervals contain the same number of pages. In extreme cases, the total number of pages is smaller than the given number of pages, the division or cut result will always have only one interval. Fortunately, this does not interfere with our core algorithms, but we still need to pay attention to code robustness. Therefore, we should first consider extreme situations. Before using algorithms to solve core problems, we should quickly capture only one page of intervals.
Next, we can look at the inherent attributes of the interval. Each dynamic cut interval has a start page and a last page. Because the intervals are sequentially arranged, after static division, we always get the first one (first) the interval and the last (end) interval. If the first and end intervals overlap, the total number of pages is less than the given page turning interval length. In any case, the key problem to be solved by the algorithm is how to locate the start and end pages of the interval. Once the two elements are identified, you can use the range function built in PHP to generate all the pages in the interval.
The algorithm compares the current page $ currentPage with the page turning interval $ displaySize to determine the position of the current page in the interval, and then exports the offset between the start page and the end page and the current page. To be perfect, we also need to consider the border overflow problem. This is also very simple. You only need to determine whether the start page and the end page are between 1 and the total page.
So far, the code analysis has been completed. Let's take a look at the time efficiency of the algorithm. Generally speaking, the basic operation in the algorithm is Modulo. The execution time of the algorithm depends on the difference between $ currentPage and $ displaySize. The larger the difference, the larger the number of modulus, the longer the execution time, the linear structure. The actual execution result is completed in an instant.
Next, we will combine the above Google search result page and use the getPageRange function to generate a paging 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 that this is completely different from the Google search result page. Yes, because Google's paging interval forces the current page to be in the middle! Well, don't be discouraged. We can still use the getPageRange function to get matching results. Just break down the problem:
Print_r (implode (',', array_merge (getPageRange (17, 17, 10), getPageRange (27, 27, 10); the result is:
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27