PHP long article paging principle and implementation Program _php tutorial

Source: Internet
Author: User
Below I analyze the long article pagination and the article page program code, the need for friends can refer to.

Method one, according to the word control for paging

Page by word method is easy to use, but the effect is not good.

General idea: First, set the maximum number of words per page, then, calculate the total number of words in the article, and then the total number of words and the maximum number of pages to calculate the total number of pages. So the whole page is ready for the job.

Specific to each page of the display can be achieved through content interception. For example: page accommodates 500 words, the article content has 2200 words, then when the page passes page=2 should display the content between the No. 501 to 1000th.

This method is simple, but the display may encounter trouble, the article content is usually accompanied by HTML tags, the content is cut to achieve the closure of the HTML tag is a certain difficulty, if the work is not done, then the effect of paging is obviously not good.

The code is as follows Copy Code


$content 1 = "The content is very long ... ";

$current =$_request[' page_t '];
$result =ff_page ($content 1, $current);
echo $result;

function Ff_page ($content, $page)
{
Global $expert _id;

if (empty ($page)) {
$page = 1;
}//Assign initial value to $page

$PageLength = 2000; Words per page
$CLength = strlen ($content); Article length
$PageCount = Floor (($CLength/$PageLength) + 1; Count pages
$PageArray =array ();//broken page position array
$Seperator = Array ("n", "R", ". ","!","?",";",",","”","’",".","! ","? ",";"); Delimited symbols

echo "pages:". $PageCount. "
";
echo "Length:". $CLength. "
";
The Strpos () function returns the position of the first occurrence of a string in another string

if ($CLength <= $PageLength)
{
Echo $content;
}//If there is only one page, print directly
else{
$PageArray [0]=0;
$Pos = 0;
$i = 0;
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 starting point for finding a position to prevent the total number of characters from being exceeded
$Pos = Strpos ($content, $Seperator [$j], $start _p);
}
If you have found a paging point, jump 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 = "(prev.)." ($page-1). " page) n ". $output;
}

Echo Str_replace ("n", "
", $output); Return to line, adjust as needed
Echo $output;

if ($PageCount > 1)
{
echo "

";
echo "". $page. " /". $PageCount." Page ";
if ($page >1)
echo "Prev";
Else
echo "Prev";

for ($i =1; $i <= $PageCount; $i + +)
{
echo "[". $i. "]";
}

if ($page < $PageCount)
echo "Next page";
Else
echo "Next page";
echo " ";
}
}
}

?>


Method Two, paging through the page break

Paging through page breaks is more desirable than the first approach.

General idea: When you edit the content of the article, insert a page break (for example:) in the content, split the content of the article when it is displayed, each part represents the content of a page, and control the page by passing parameters.

This approach is more humane, after all, through the manual control of the content of the interception of the whole of our thinking, and to a certain extent, to avoid the HTML tag does not close the situation.

The article content pagination code, is based on the page character inserted by the editor to operate, we can divide the beautiful offset effect of pagination.

The code is as follows Copy Code

Class Contentpage
{
Private $content; Article content
Private $pagesize; Minimum number of bytes per page
Private $breakflag; Page break (can be customized, default is N)
Private $pageurl; URL address
Private $pagevar; Paging parameters
Public $pagecount; Total pages
Public $page; Current page number
Public $pagebreak; Start position per page

function __construct ($content = "", $pagesize = ten, $breakflag = "n", $pageurl = ", $pagevar = ' P ')
{
$this->content = $content;
$this->pagesize = $pagesize;
$this->breakflag = $breakflag;
$this->pageurl = $pageurl;
$this->pagevar = $pagevar;
$this->getpages ();
}

Total pages, starting and ending positions for each page
Public Function GetPages ()
{
$contentlen = strlen ($this->content); 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 $this->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 per page
function GetPage ()
{
Intercept data for 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 = "Geturl ()." =". ($this->page-1). "' class= ' div ' > prev ';
}

Output Digital Page number
for ($j =1; $j <= $this->pagecount; $j + +)
{
if ($j = = $this->page)
{
$pagenav. = "". $j."";
}
Else
{
$pagenav. = "Geturl ()." = ". $j." ' class= ' div ' > '. $j. ' ";
}
}
Next page
if ($this->page < $this->pagecount && $this->pagecount >1)
{
$pagenav. = "Geturl ()." =". ($this->page+1). "' class= ' div ' > next page ";
}
return $pagenav;
}
Get URL Address
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 = "First page: article content page Aston wasted space Aston, Fujian province, Jilin Province, Fujian Road, near the mouth of the large hand-drawn aircraft waste supervision costs
second page: Aston Room a mutual understanding ah to Sara development remember who to pay will be the other side of the space to Fujian Ali whether
The third page: Owen ah oh, look at the field to continue the Super Article content page filter Blue Card
Fourth: Owenzhi according to Lhasa and launched four points ah on both sides will love is found to be the content of the article page No. ";
$model = new Contentpage ($content);
Echo $model->getpage ();//Output paging content
Echo $model->getpagenav ();//output page number
?

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

http://www.bkjia.com/PHPjc/631578.html www.bkjia.com true http://www.bkjia.com/PHPjc/631578.html techarticle below I analyze the long article pagination and the article page program code, the need for friends can refer to. Method one, according to the word control to pagination by the Word page method is simple and easy to use, but the effect of ...

  • Related Article

    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.