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.