pagination algorithm for 3 lines of code (find start and end pages)

Source: Internet
Author: User
Tags min

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



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.