When pagination is involved, the start page and end page must be calculated unless only the previous or next page is displayed. I have seen that a lot of code is implemented with a lot of if-else, which is large in size and not concise. an algorithm that only requires three lines of code is provided. A good paging algorithm should have... "> <LINKhref =" http://www.php100.com//statics/styl
When pagination is involved, the start page and end page must be calculated unless only the previous or next page is displayed. I have seen that a lot of code is implemented with a lot of if-else, which is large in size and not concise. an algorithm that only requires three lines of code is provided.
A good paging algorithm should have the following advantages:
The current page number should be in the middle.
If the "homepage" and "last page" are unavailable (currently on the first or last page), do not hide these two groups of text to avoid link button location changes.
The algorithm is simple.
The following algorithms have two advantages: 1 and 3.
PHP:
// $ Curr_index, current page number.
// $ Link_count, number of links.
// $ Page_count: the total number of pages of the current data.
// $ Start indicates the start page number.
// $ End: the ending page number displayed.
$ Start = max (1, $ curr_index-intval ($ link_count/2 ));
$ End = min ($ start + $ link_count-1, $ page_count );
$ Start = max (1, $ end-$ link_count + 1 );
JavaScript:
Start = Math. max (1, curr_index-parseInt (link_count/2 ));
End = Math. min (page_count, start + link_count-1 );
Start = Math. max (1, end-link_count + 1 );