Home > Article > Backend Development > PHP article content pagination code_PHP tutorial
The PHP article content pagination code operates based on the page breaks inserted by the editor. We can create super beautiful offset effect pagination. Article content pagination Article content pagination
php tutorial The article content pagination code operates based on the page breaks inserted by the editor. We can create super beautiful offset effect pagination.
Article content pagination
Article content pagination
* author: Chen Kai
* data: 2010-09-15
* Article pagination category
*/
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 number of pages
public $page; //Current page number
public $pagebreak; //Starting position of each pagefunction __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, The starting and ending positions of each page
public function getpages()
{
$contentlen = strlen($this->content); //The total number of bytes of 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 $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 of each page
function getpage()
{
//Intercept 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]);
}}
//Page break
public function getpagenav()
{
if($this->page > 1)
{
$pagenav = "Previous page ";
}//输出数字页码
for($j=1;$j<=$this->pagecount;$j++)
{
if($j == $this->page)
{
$pagenav .= "".$j." ";
}
else
{
$pagenav .= "".$j." ";
}
}
//下一页
if($this->page < $this->pagecount && $this->pagecount >1)
{
$pagenav .= "下一页 ";
}
return $pagenav;
}
//获取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 = "第一页:文章内容分页阿斯顿浪费空间阿斯顿来看福建省地方吉林省福建路口附近大手拉飞机上浪费的说浪费监理费
第二页:阿斯顿房间阿双方了解啊对萨拉开发记得谁来付款将令对方空间的来福建阿里是否
第三页:欧文炯诶哦生地拉开方面来看就继续超文章内容分页滤机蓝卡
第四页:欧文日据拉萨及发动四分啊就双方的将爱是发觉是文章内容分页否了。";
$model = new contentpage($content);
echo $model->getpage(); //输出分页内容
echo $model->getpagenav(); //输出页码
?>