When it comes to paging, you need to calculate the start and end pages unless only the previous page/next page is displayed. See a lot of code is a lot of if-else to achieve, the code is large, and not concise. Now provides an algorithm that requires only 3 lines of code.
A good page-splitting algorithm should have the following advantages:
The current page number should be as far as possible in the middle.
If "first" and "last" are not available (currently on the first or last page), do not hide the two groups of text so that the link button position changes.
The algorithm is simple.
The following algorithm has the preceding 1 and 32 advantages.
PHP:
$curr _index, current page number.
$link _count, number of links.
$page _count, the total number of pages of the current data.
$start The starting page number when it is displayed.
$end, the ending page number when 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);