Page number calculation method

Source: Internet
Author: User
When developing search engines and other applications, it is necessary to provide a flip-page navigation bar. I have read some related code on the Internet and it is very complicated. Dizzy ~~~ In fact, its mathematical formula is very simple. This article provides the two most commonly used algorithms.

Paging

The style is as follows. 10 page numbers are displayed each time, and "Top 10" and "last 10" pages are provided. [1] 2 3 4 5 6 7 8 9 10 next 10 pages last page
1 2 3 4 5 6 [7] 8 9 10 next 10 pages last page
1 2 3 4 5 6 7 8 9 [10] Next 10 pages last page
Top 10 pages on the home page [11] 12 13 14 15 16 18 19 20 bottom 10 pages last pages
Top 10 pages 11 12 13 14 15 [16] 17 18 19 20 bottom 10 pages last pages

Calculation formula: (the minimum value of the current page number is 1) int x = the current page number/10;
If (current page number % 10 = 0) -- X;
Int startpage = (x * 10) + 1;
Int endpage = math. Min (total number of pages, startpage + 9 );

Generate paging Navigation Code Demonstration: Private string getnavbarhtml (string S, int pageindex, int pagecount)
{
// Calculate the displayed page number
Int x = pageindex/10;
If (pageindex % 10 = 0) -- X;
Int startpage = (x * 10) + 1;
Int endpage = math. Min (pagecount, startpage + 9 );

// Generate the form feed code
System. Text. stringbuilder sb = new stringbuilder ();
String url = "<a href = \" search. aspx? S = {1} & page = {2} \ "> {0} </a> ";

If (startpage> 1)
{
SB. append (string. Format (URL, "Homepage", S, 1 ));
SB. append ("& nbsp ");
SB. append (string. Format (URL, "Top 10", S, startpage-1 ));
SB. append ("& nbsp ");
}

For (INT I = startpage; I <= endpage; I ++)
{
If (I! = Pageindex)
SB. append (string. Format (URL, I, S, I ));
Else
SB. append (string. Format ("[{0}]", I ));

SB. append ("& nbsp ");
}

If (pagecount> endpage)
{
SB. append (string. Format (URL, "Last 10 pages", S, endpage + 1 ));
SB. append ("& nbsp ");
SB. append (string. Format (URL, "Last page", S, pagecount ));
}

Return sb. tostring ();
}

Rolling

The style is as follows. Place the current page number in the middle and scroll through the two pages. [1] 2 3 4 5 6 7 8 9 10 last pages
1 2 3 4 [5] 6 7 8 9 10 last pages
Homepage 2 3 4 5 [6] 7 8 9 10 11 last page
Home Page 5 6 7 8 [9] 10 11 12 13 14 last page
Home Page 8 9 10 11 [12] 13 14 15 16 17 last page

Calculation formula: (the minimum value of the current page number is 1) int startpage = math. Max (the current page number-4, 1 );
Int endpage = math. Min (total number of pages, startpage + 9 );

Generate rolling Navigation Code Demonstration: Private string getnavbarhtml (string S, int pageindex, int pagecount)
{
// Calculate the displayed page number
Int startpage = math. Max (pageindex-4, 1 );
Int endpage = math. Min (pagecount, startpage + 9 );

// Generate the form feed code
System. Text. stringbuilder sb = new stringbuilder ();
String url = "<a href = \" search. aspx? S = {1} & page = {2} \ "> {0} </a> ";

If (startpage> 1)
{
SB. append (string. Format (URL, "Homepage", S, 1 ));
SB. append ("& nbsp ");
}

For (INT I = startpage; I <= endpage; I ++)
{
If (I! = Pageindex)
SB. append (string. Format (URL, I, S, I ));
Else
SB. append (string. Format ("[{0}]", I ));

SB. append ("& nbsp ");
}

If (pagecount> endpage)
{
SB. append (string. Format (URL, "Last page", S, pagecount ));
}

Return sb. tostring ();
}

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.