3-line code paging algorithm (for the start page and end page)

Source: Internet
Author: User
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 );

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.