Paging Long articles using PHP

Source: Internet
Author: User

How to split pages

Manual paging: When you edit content, you typically add special paging tags, such as {pages}, and the PHP program processes pagination based on page breaks to generate different static pages. This paging method is accurate, but requires manual page breaks to manually add a lot of work.

Automatic paging: The PHP program pages the content according to a set page break, and then generates a different static page. This method is highly efficient and requires a high level of processing of different HTML code tags.

Front-end JS paging: Use JavaScript to intercept the long article content, according to the request to show different segmented content, to achieve the paging effect. This method will read the content at once, by the front-end JS processing paging, experience good.

The example code in this article explains the use of PHP to page long article content, automatic and manual paging. As for generating static HTML pages is not covered in the scope of this article, we will specifically explain the generation of static aspects of the article introduction.

Pagination Class

<?PHP/** Long article pagination class*/       classcutpage{Private $pagestr;//what's being sliced        Private $pagearr;//array format for sliced text        Private $sum _word;//Total Words (Chinese characters in the UTF-8 format are also included)        Private $sum _page;//Total Pages        Private $page _word;//How many words a page        Private $cut _tag;//automatic page Breaks        Private $cut _custom;//manual page Break        Private $ipage;//Number of pages currently sliced, page no.        Private $url; function__construct ($pagestr,$page _word=1000){                 $this->page_word =$page _word; $this->cut_tag =Array("</table>", "</div>", "</p>", "<br/>", "".) ", "。 ", ".", "! ", "......", "? ", ","); $this->cut_custom = "{nextpage}"; $tmp _page=intval(Trim($_get["IPage"])); $this->ipage =$tmp _page>1?$tmp _page: 1; $this->pagestr =$pagestr; }                      functionCut_str () {$str _len_word=strlen($this-&GT;PAGESTR);//gets the total number of characters that are obtained using strlen            $i= 0; if($str _len_word<=$this->page_word) {//if the total word count is less than one page display word Count                $page _arr[$i] =$this-Pagestr; }Else{                     if(Strpos($this-&GT;PAGESTR,$this-Cut_custom)) {                         $page _arr=Explode($this->cut_custom,$this-pagestr); }Else{                         $str _first=substr($this-&GT;PAGESTR, 0,$this->page_word);//0-page_word characters cutstr as functions in Func.global                    foreach($this->cut_tag as $v){                             $cut _start=Strrpos($str _first,$v);//reverse Find the location of the first page break                        if($cut _start){                                 $page _arr[$i++] =substr($this-&GT;PAGESTR, 0,$cut _start).$v; $cut _start=$cut _start+strlen($v);  Break; }                         }                         if(($cut _start+$this->page_word) >=$str _len_word){//if the total number of words is exceeded                        $page _arr[$i++] =substr($this-&GT;PAGESTR,$cut _start,$this-Page_word); }Else{                              while(($cut _start+$this->page_word) <$str _len_word){                                 foreach($this->cut_tag as $v){                                     $str _tmp=substr($this-&GT;PAGESTR,$cut _start,$this->page_word);//take the Page_word character after the first cut_start word                                $cut _tmp=Strrpos($str _tmp,$v);//find the location of the first page break from the Page_word word after Cut_start                                if($cut _tmp){                                         $page _arr[$i++] =substr($str _tmp, 0,$cut _tmp).$v; $cut _start=$cut _start+$cut _tmp+strlen($v);  Break; }                                 }                               }                             if(($cut _start+$this->page_word) >$str _len_word){                                 $page _arr[$i++] =substr($this-&GT;PAGESTR,$cut _start,$this-Page_word); }                         }                     }                 }                 $this->sum_page =Count($page _arr);//Total Pages            $this->pagearr =$page _arr; return $page _arr; }             //Show Previous, next        functionPagenav () {$this-Set_url (); $str= ' '; //$str. = $this->ipage. ' /'. $this->sum_page;                           for($i= 1;$i<=$this->sum_page;$i++){                 if($i==$this-ipage) {                     $str. = "<a href= ' # ' class= ' cur ' >".$i." </a> "; }Else{                     $str. = "<a href= '".$this->url.$i."‘ > ".$i." </a> "; }             }                                              return $str; }                        functionSet_url () {Parse_str($_server["Query_string"],$arr _url); unset($arr _url["IPage"]); if(Empty($arr _url)){                     $str= "Ipage="; }Else{                     $str=Http_build_query($arr _url)." &ipage= "; }                 $this->url = "http://".$_server["Http_host"].$_server["Php_self"]. "?".$str; }         }     ?>

The above Cutpage class can handle content paging very well, and can handle the trouble of different HTML tags for paging. If the content is set to page break {nextpage}, the content is automatically paginated by page breaks.

calling a paging class

We assume that the content of the file Text.txt is read, and the actual project should be the form submitting long content or reading the contents of the database related table. It then instantiates the paging class and then invokes the corresponding paging content and output, along with the output paging bar, based on the current page.

<?PHP$content=file_get_contents(' Text.txt '); $ipage=$_get["IPage"]?intval($_get["IPage"]) : 1; $CP=NewCutpage ($content); $page=$CP-Cut_str (); Echo $page[$ipage-1]; Echo $CP-Pagenav ();?>

It's worth noting that using a unified UTF-8 file encoding will make your coding work smoother.

Disclaimer: This article is original article, helloweba.com and author have copyright, if need to reprint, please indicate from helloweba.com and keep original link: http://www.helloweba.com/view-blog-346.html

Paging Long articles using PHP

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.