Php long article paging Principle and Implementation Program

Source: Internet
Author: User

Next I will analyze the code of long article paging and article paging programs. If you need code, refer to it.

Method 1. Paging by word count

Paging by words is easy to use, but the effect is poor.

General idea: first, set the maximum number of words per page. Then, calculate the total number of words in the article content, and then calculate the total number of pages based on the total number of words and the maximum number of words on a single page. In this way, the preparation for the entire page is ready.

The content displayed on each page can be captured by content. For example, if the page contains 500 words and the article content contains 2200 words, the content between 501st and 1000 should be displayed when page = 2 is passed.

This method is simple, but may be difficult to display. The content of the article is usually accompanied by HTML tags. It is difficult to close HTML tags when cutting content. If this is not done well, the effect after paging is obviously not good.

The Code is as follows: Copy code

<? Php


$ Content1 = "the content is very long ...... ";

$ Current = $ _ REQUEST ['page _ T'];
$ Result = ff_page ($ content1, $ current );
Echo $ result;

Function ff_page ($ content, $ page)
{
Global $ expert_id;

If (empty ($ page )){
$ Page = 1;
} // Assign an initial value to $ page

$ PageLength = 2000; // Number of words per page
$ CLength = strlen ($ content); // Article Length
$ PageCount = floor ($ CLength/$ PageLength) + 1; // calculate the page number
$ PageArray = array (); // array of broken page locations
$ Seperator = array ("n", "r ",". ","! ","? ",";",",",""","'",".","! ","? ","; "); // Separator number

// Echo "page number:". $ PageCount. "<br> ";
// Echo "Length:". $ CLength. "<br> ";
// The strpos () function returns the position where the string first appears in another string.

If ($ CLength <= $ PageLength)
{
Echo $ content;
} // Print only one page
Else {
$ PageArray [0] = 0;
$ Pos = 0;
$ I = 0;
// The first page, print_r ($ Seperator );
For ($ j = 0; $ j <sizeof ($ Seperator); $ j ++)
{
$ Pos = strpos ($ content, $ Seperator [$ j], $ PageArray [$ I] + 1900 );
While ($ Pos> 0 & $ Pos <($ I + 1) * $ PageLength & $ Pos> $ I * $ PageLength)
{
$ PageArray [$ I] = $ Pos;
If ($ Pos + $ PageLength> $ CLength)
{
$ Start_p = $ CLength-1;
}
Else {
$ Start_p = $ Pos + $ PageLength;
}
// Give a start point for the position to be searched to prevent exceeding the total number of characters in the position
$ Pos = strpos ($ content, $ Seperator [$ j], $ start_p );
}
// If a paging point is found, the page jumps out of the loop.
If ($ PageArray [$ I]> 0)
{
$ J = $ j + sizeof ($ Seperator) + 1;
}
}

For ($ I = 1; $ I <$ PageCount-1; $ I ++)
{
For ($ j = 0; $ j <sizeof ($ Seperator); $ j ++)
{
$ Pos = strpos ($ content, $ Seperator [$ j], $ PageArray [$ I-1] + 1900 );
While ($ Pos> 0 & $ Pos <($ I + 1) * $ PageLength & $ Pos> $ I * $ PageLength)
{
$ PageArray [$ I] = $ Pos;
If ($ Pos + $ PageLength> $ CLength)
{
$ Start_p2 = $ CLength-1;
}
Else {
$ Start_p2 = $ Pos + $ PageLength;
}
$ Pos = strpos ($ content, $ Seperator [$ j], $ start_p2 );
}
If ($ PageArray [$ I]> 0)
{
$ J = $ j + sizeof ($ Seperator) + 1;
}
}
}
// -- PHP long article paging function last page
$ PageArray [$ PageCount-1] = $ CLength;
// $ Page = 2;

If ($ page = 1)
{
$ Output = substr ($ content, 0, $ PageArray [$ page-1] + 2 );
}
If ($ page> 1 & $ page <= $ PageCount)
{
$ Output = substr ($ content, $ PageArray [$ page-2] + 2, $ PageArray [$ page-1]-$ PageArray [$ page-2]);
$ Output = "(connected to the". ($ page-1). "page) n". $ output;
}

// Echo str_replace ("n", "<br> & nbsp;", $ output); // press enter to wrap the line and adjust it as needed
Echo $ output;

If ($ PageCount> 1)
{
Echo "<br> <center> ";
Echo "<font color = 'ff000000'>". $ page. "</font>/". $ PageCount. "page & nbsp ;";
If ($ page> 1)
Echo "<a href = $ PHP_SELF? Expert_id = $ expert_id & page_t = ". ($ page-1)."> previous page </a> ";
Else
Echo "Previous Page ";

For ($ I = 1; $ I <= $ PageCount; $ I ++)
{
Echo "<a href = $ PHP_SELF? Expert_id = $ expert_id & page_t = ". $ I."> [". $ I."] </a> ";
}

If ($ page <$ PageCount)
Echo "<a href = $ PHP_SELF? Expert_id = $ expert_id & page_t = ". ($ page + 1)."> next page </a> ";
Else
Echo "next page ";
Echo "</center> ";
}
}
}

?>


Method 2: Paging by pagination

Paging with a paging character is more ideal than the first method.

General idea: Insert a pagination character (for example,

This method is more user-friendly. After all, the paging content captured by manual control is more full of our thinking, And the HTML Tag cannot be closed to some extent.

The pagination code of the article content is operated based on the pagination characters inserted by the editor. We can split the pages with ultra-beautiful offset effects.

The Code is as follows: Copy code

Class contentpage
{
Private $ content; // document content
Private $ pagesize; // minimum number of bytes per page
Private $ breakflag; // Page Break (customizable, default: n)
Private $ pageurl; // url
Private $ pagevar; // page Parameters
Public $ pagecount; // the total number of pages.
Public $ page; // the current page number.
Public $ pagebreak; // start position of each page

Function _ construct ($ content = "", $ pagesize = 10, $ breakflag = "n", $ pageurl = '', $ pagevar = 'P ')
{
$ This-> content = $ content;
$ This-> pagesize = $ pagesize;
$ This-> breakflag = $ breakflag;
$ This-> pageurl = $ pageurl;
$ This-> pagevar = $ pagevar;
$ This-> getpages ();
}

// Total number of pages, start position and end position of each page
Public function getpages ()
{
$ Contentlen = strlen ($ this-> content); // The total number of bytes in the article
$ This-> pagebreak [0] = 0;
$ I = 0;
$ Offset = $ this-> pagesize;

For ($ k = 0; $ k <$ contentlen/$ this-> pagesize; $ k ++)
{
If ($ offset> $ contentlen)
{
$ I ++;
$ This-> pagebreak [$ I] = $ contentlen;
Break;
}
// Find the location where the pagevar appears.
$ Where = strpos ($ this-> content, $ this-> breakflag, $ offset );
If ($ where> $ contentlen or intval ($ where) <1)
{
$ I ++;
$ This-> pagebreak [$ I] = $ contentlen;
Break;
}
Else
{
$ I ++;
$ This-> pagebreak [$ I] = $ where;
$ Offset = $ where + $ this-> pagesize;
}
}
$ This-> pagecount = $ I;
If (isset ($ _ get [$ this-> pagevar]) & $ _ get [$ this-> pagevar]> 1 & $ _ get [$ this-> pagevar] <= $ this-> pagecount)
{
$ This-> page =$ _ get [$ this-> pagevar];
}
Else
{
$ This-> page = 1;
}
}

// Content on each page
Function getpage ()
{
// Truncate the data of the current page number
If ($ this-> page> 1)
{
Return substr ($ this-> content, $ this-> pagebreak [$ this-> page-1] + 1, $ this-> pagebreak [$ this-> page]-$ this-> pagebreak [$ this-> page-1]);
}
Else
{
Return substr ($ this-> content, $ this-> pagebreak [$ this-> page-1], $ this-> pagebreak [$ this-> page]-$ this-> pagebreak [$ this-> page-1]);
}

}

// Pagination bar
Public function getpagenav ()
{
If ($ this-> page> 1)
{
$ Pagenav = "<a href = '". $ this-> geturl (). "= ". ($ this-> page-1 ). "'class = 'div '> previous page </a> & nbsp ;";
}

// Output the number page number
For ($ j = 1; $ j <= $ this-> pagecount; $ j ++)
{
If ($ j = $ this-> page)
{
$ Pagenav. = "<span class = 'divsi'>". $ j. "</span> & nbsp ;";
}
Else
{
$ Pagenav. = "<a href = '". $ this-> geturl (). "= ". $ j. "'class = 'div '> ". $ j. "</a> & nbsp ;";
}
}
// Next page
If ($ this-> page <$ this-> pagecount & $ this-> pagecount> 1)
{
$ Pagenav. = "<a href = '". $ this-> geturl (). "= ". ($ this-> page + 1 ). "'class = 'div '> next page </a> & nbsp ;";
}
Return $ pagenav;
}
// Obtain the url
Public function geturl ()
{
$ Url = $ _ server ['request _ uri '];
$ Parse_url = parse_url ($ url );
$ Query_url = $ parse_url ['query'];

If ($ query_url)
{
$ Query_url = ereg_replace ("(^ | &)". $ this-> pagevar. "=". $ this-> page, "", $ query_url );
$ Url = str_replace ($ parse_url ['query'], $ query_url, $ url );
If ($ query_url)
{
$ Url. = "&". $ this-> pagevar;
}
Else
{
$ Url. = $ this-> pagevar;
}
}
Else
{
$ Url. = "? ". $ This-> pagevar;
}
Return $ url;
}
}

$ Content = "Page 1: The article content is displayed in pages. Aston is a waste of space. Aston is a waste of supervision fees.
Page 2: Aston room. Both parties know. For saradevelopment, remember who will make the payment so that the other party's space will come to Fujian and Alibaba?
Page 3
Page 4: Irwin detected that the content of the article is not displayed on pages according to Lhasa and launched by both parties. ";
$ Model = new contentpage ($ content );
Echo $ model-> getpage (); // output the page content
Echo $ model-> getpagenav (); // output page number
?>

<Style type = "text/css tutorial">
<! --
Body {
Width: 800px;
Margin: 0 auto;
Margin-top: 50px;
Font-size: 12px;
}
A {
Color: # 014ccc;
Text-decoration: none;
}
. Div {
Float: left;
Cursor: pointer;
Font-weight: bold;
Margin-right: 5px;
Display: block;
Padding: 3px 7px;
Text-align: center;
Border: # bbdded solid 1px;
}
. Divs {
Float: left;
Font-weight: bold;
Margin-right: 5px;
Display: block;
Padding: 3px 7px;
Text-align: center;
Border: # cccccc solid 1px;
}
. Divsi {
Float: left;
Font-weight: bold;
Margin-right: 5px;
Display: block;
Padding: 3px 7px;
Text-align: center;
Background: # 3399ff;
Color: # ffffff;
Border: # cccccc solid 1px;
}
. Div: hover {
Background: # 3399ff;
Color: # ffffff;
}
. Divsi: hover {
Background: # 3399ff;
Color: # ffffff;
}
-->
</Style>

For more details, see http://www.bKjia. c0m/phper/php-gj/35233.htm

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.