» » Use PHP to paginate long articles

 

Use PHP to paginate long articles

Author: bamboo06 on 6-10-2016, 12:28, views: 1509

8
When the article content expertise, in order to facilitate reading and page display content we usually paged to display. The general page processing is published in the background when the article will be submitted to generate multiple pages after the static file. In this paper, we will use examples to explain the dynamic use of PHP long page content paging.


How to page
Manual paging: the general content in the edit when adding special paging tags, such as {pages}, submitted, the PHP program will process page breaks according to the page break, generate a different static pages. This sort of paging method is accurate, but need to manually add page breaks, a heavy workload.
Automatic paging: PHP program will be set according to the page break the content of the page, and then generate a different static pages. The method is efficient and requires a high tag for handling different html tags.
Front-end JS page: the use of javascript will be long article content interception segment, according to the request to display different sub-content, to achieve paging effect. This method will read the contents of the first by the front-end js paging, experience good.
This example code is to explain the use of PHP to long article content paging, automatic and manual paging. As for the static html page is not generated within the scope of this article, we will be devoted to the static aspects of the article.

Paging class
<?php    
/*  
*  Long article paging class    
*/  
    class cutpage{    
        private $pagestr;       //Segmented content   
        private $pagearr;       //The format of the cut-off text array    
        private $sum_word;      //Total number of words  
        private $sum_page;      //Total page num   
        private $page_word;     //Word num per page    
        private $cut_tag;       //Automatic page break    
        private $cut_custom;    //Manual page breaks    
        private $ipage;         //The number of pages of the current cut   
        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;
        }    
        //Statistics total words   
        function get_page_word(){    
            $this->sum_word = $this->strlen_utf8($this->pagestr);    
            return $this->sum_word;    
        }    
        /*  Counts the character length of the UTF-8 encoding  
         */  
        function strlen_utf8($str){    
           $i = 0;    
           $count = 0;    
           $len = strlen ($str);    
           while ($i < $len){    
               $chr = ord ($str[$i]);    
               $count++;    
               $i++;    
               if ($i >= $len)    
                   break;    
               if ($chr & 0x80){    
                   $chr <<= 1;    
                   while ($chr & 0x80) {    
                       $i++;    
                       $chr <<= 1;    
                   }    
               }    
           }    
           return $count;    
        }    
        //Sets the automatic page break   
        function set_cut_tag($tag_arr=array()){    
            $this->cut_tag = $tag_arr;    
        }    
        //Set the manual page break   
        function set_cut_custom($cut_str){    
            $this->cut_custom = $cut_str;    
        }    
        function show_cpage($ipage=0){    
            $this->cut_str();    
            $ipage = $ipage ? $ipage:$this->ipage;    
            return $this->pagearr[$ipage];    
        }    
        function cut_str(){    
            $str_len_word = strlen($this->pagestr);     //Gets the total number of characters obtained using strlen   
            $i = 0;    
            if ($str_len_word<=$this->page_word){   //If the total number of words is less than a page display number of words  
                $page_arr[$i] = $this->pagestr;    
            }else{    
                if (strpos($this->pagestr, $this->cut_custom)){    
                    $page_arr = explode($this->cut_custom, $this->pagestr);    
                }else{    
                    $str_first = substr($this->pagestr, 0, $this->page_word);   //0-page_word The words cutStr are functions in func.global
                    foreach ($this->cut_tag as $v){    
                        $cut_start = strrpos($str_first, $v);       //Looks up the position of the first page break   
                        if ($cut_start){    
                            $page_arr[$i++] = substr($this->pagestr, 0, $cut_start).$v;    
                            $cut_start = $cut_start + strlen($v);    
                            break;    
                        }    
                    }    
                    if (($cut_start+$this->page_word)>=$str_len_word){  //If more than the total number of words  
                        $page_arr[$i++] = substr($this->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->pagestr, $cut_start, $this->page_word);        //The page_word characters after the first cut start character  
                                $cut_tmp = strrpos($str_tmp, $v);       //Find out from the first word after the cut start, page word between the words, reverse look at the location of the first page break   
                                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->pagestr, $cut_start, $this->page_word);    
                        }    
                    }    
                }    
            }    
            $this->sum_page = count($page_arr);     //total pages   
            $this->pagearr = $page_arr;  
			return $page_arr;
        }    
        //Show Previous, Next  
        function pagenav(){    
            $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;    
        }    
		function show_prv_next2(){    
            $this->set_url();    
            $str = '';
			
			
			
            if ($this->sum_page>1 and $this->ipage>1){    
                $str.= "<a href='".$this->url.($this->ipage-1)."'>Previous page</a> ";    
            }   
			if ($this->sum_page>1 and $this->ipage<$this->sum_page){    
                $str .= "<a href='".$this->url.($this->ipage+1)."'>Next page</a>";    
            }   			
            return $str;    
        }    
        function show_page_select(){    
            if ($this->sum_page>1){    
                $str = "   <select onchange='location.href=this.options[this.selectedIndex].value'>";    
                for ($i=1; $i<=$this->sum_page; $i++){    
                    $str.= "<option value='".$this->url.$i."' ".(($this->ipage)==$i ? " selected='selected'":"").">Page ".$i."</option>";    
                }    
                $str.= "</select>";    
            }    
            return $str;    
        }    
        function show_page_select_wap(){    
            if ($this->sum_page>1){    
                $str = "<select ivalue='".($this->ipage-1)."'>";    
                for ($i=1; $i<=$this->sum_page; $i++){    
                    $str.= "<option onpick='".$this->url.$i."'>Section ".$i."</option>";    
                }    
                $str.= "</select>";    
            }    
            return $str;    
        }    
        function set_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 be very good handling content paging, can handle different html tags to pagination trouble. If the content has a page break {nextpage} set, the content is automatically sorted by page break.

Call the paging class
We assume that the text of the article read the text.txt the contents of the actual project should be submitted to the form of long content or read the contents of the database-related tables. And then instantiate the paging class, and then call the corresponding page under the current page content and output, as well as the output tab.
<?php 
$content = file_get_contents('text.txt');     
    $ipage = $_GET["ipage"]? intval($_GET["ipage"]):1;     
    $CP = new cutpage($content);     
    $page = $CP->cut_str();   
    echo $page[$ipage-1];    
    echo $CP->pagenav(); 
?> 

It is worth noting that the use of a unified UTF-8 file encoding, will make your coding work more smoothly.

Category: PHP Scripts

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
3 October 2017 09:52

ali

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
In this article understand the most important thing, the item will give you a keyword rich link a great useful website page: Stroke by Stroke

<
  • 0 Comments
  • 0 Articles
3 October 2017 23:05

imran

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
wow this saintly however ,I love your enter plus nice pics might be part personss negative love being defrent mind total poeple , The Superior Singing Method

<
  • 0 Comments
  • 0 Articles
4 October 2017 00:44

ustad

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
It is rather very good, nevertheless glance at the data with this handle. Her Horny

<
  • 0 Comments
  • 0 Articles
9 October 2017 22:17

john

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Really cool stuff. Didn't know that PHP is so useful. I am an expert in essay writing services and looking forward to apply PHP to paginate my lengthy articles.

<
  • 0 Comments
  • 0 Articles
21 December 2017 23:27

AdrianAdrien

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Though Php is my favorite one but still i am unable to cope up the situation and would definitely help lot of people if they need through our amandawriter services, we hope a change in our society, so we do. I am amazed to work on your website and would help you through out my services, thanks for sharing this with us.

<
  • 0 Comments
  • 0 Articles
2 January 2018 16:06

voilettaylor

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Php is such an important language. It helps mostly people to change their work. Are you looking for help with assignment writing solutions in the internet to get online help from professional assignment writing services? We hire only certified writers with advanced degrees to assist you in the best possible way. High quality is a guarantee.

<
  • 1 Comment
  • 0 Articles
2 February 2018 15:27

liana

Reply
  • Group: Members
  • РRegistered date: 2.02.2018
  • Status: Currently Offline
 
awesome post, i got some great information about PHP, thanks for sharing free movies

<
  • 0 Comments
  • 0 Articles
5 March 2018 14:41

rolex replica watches

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Star watch looks very fake rolex watche personal, black main tone and cool, using high-tech replica rolex ceramic material, emitting a replica watches uk unique luster. Dials on the dial with 12 diamond time scales, and the uk replica watches rose golden pointer with a glow.

Information
Comment on the news site is possible only within (days) days from the date of publication.