办法一、按字数控制进行分页
按字数分页办法简单易用,但效果不好。
大致思想:首先、设定每页能容纳的最大字数;然后、计算文章内容的总字数,再由总字数和单页最大字数计算出出总页数。这样整个分页的准备工作就已经做好了。
具体到每一页的显示内容可以通过内容截取来实现。比如:页容纳500字,文章内容有2200字,那么当页面传递page=2时应该显示第501至1000之间的内容。
这种办法简单,但显示时可能会遇到麻烦,文章内容通常伴有HTML标签,进行内容切割时实现HTML标签的闭合有一定难度,如果这个工作没做好,那么分页后的效果显然也是不好的。
代码如下:
- <?php
-
- $content1="内容要很长。。。。。。";
- $current=$_REQUEST['page_t'];
- $result=ff_page($content1,$current);
- echo $result;
- function ff_page($content,$page)
- {
- global $expert_id;
-
- if (emptyempty($page)) {
- $page = 1 ;
- }
-
- $PageLength = 2000;
- $CLength = strlen($content);
- $PageCount = floor(($CLength / $PageLength)) + 1;
- $PageArray=array();
- $Seperator = array("n","r","。","!","?",";",",","”","’",".","!","?",";");
-
-
-
-
-
- if($CLength <= $PageLength)
- {
- echo $content;
- }
- else{
- $PageArray[0]=0;
- $Pos = 0;
- $i=0;
-
- 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 ;
- }
-
- $Pos = strpos($content,$Seperator[$j],$start_p) ;
- }
-
- 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;
- }
- }
- }
-
- $PageArray[$PageCount-1] = $CLength;
-
-
- 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=" (上接第".($page-1)."页)n".$output;
- }
-
-
- echo $output ;
-
- if($PageCount > 1)
- {
- echo "<br ><center>";
- echo "<font color='ff0000'>".$page."</font>/".$PageCount."页 ";
- if($page>1)
- echo "<a href=$PHP_SELF?expert_id=$expert_id&page_t=".($page-1).">上一页</a> ";
- else
- echo "上一页 ";
-
- for( $i=1 ; $i <= $PageCount ; $i++)
- {
- echo "<a href=$PHP_SELF?expert_id=$expert_id&page_t=".$i.">[".$i."]</a> ";
- }
-
- if($page < $PageCount)
- echo " <a href=$PHP_SELF?expert_id=$expert_id&page_t=".($page+1).">下一页</a> ";
- else
- echo " 下一页 ";
- echo "</center>";
- }
- }
- }
- ?>
办法二、通过分页符进行分页
通过分页符进行分页比起第一种办法要更为理想。
大致思想:在编辑文章内容时往内容里面插入分页符(如:<hr>),在文章显示时对文章内容进行分割,每一部分表示一个页的内容,通过传递参数控制显示第几页。
这种办法比较人性化,毕竟通过人工控制出来的分页截取的内容更符全我们的思维,而且可以一定程度上避免HTML标签不闭合的情形。
文章内容分页代码,是根据由编辑器插入的分页符来操作了,我们可以分出超漂亮的偏移效果的分页。
代码如下:
- <style type="text/css">
- <!--
- body {
- width:800px;
- margin:0 auto;
- margin-top:50px;
- font-size:12px;
- }
- a {
- color:#014ccc;
- text-decoration:none;
- }
- .div {
- float:left;
- cursor:pointer;
- font-weight:bold;
- margin-right:5px;
- display: block;
- padding:3px 7px;
- text-align:center;
- border:#bbdded solid 1px;
- }
- .divs {
- float:left;
- font-weight:bold;
- margin-right:5px;
- display: block;
- padding:3px 7px;
- text-align:center;
- border:#cccccc solid 1px;
- }
- .divsi {
- float:left;
- font-weight:bold;
- margin-right:5px;
- display: block;
- padding:3px 7px;
- text-align:center;
- background:#3399ff;
- color:#ffffff;
- border:#cccccc solid 1px;
- }
- .div:hover {
- background:#3399ff;
- color:#ffffff;
- }
- .divsi:hover {
- background:#3399ff;
- color:#ffffff;
- }
- -->
- </style>
- class contentpage
- {
- private $content;
- private $pagesize;
- private $breakflag;
- private $pageurl;
- private $pagevar;
- public $pagecount;
- public $page;
- public $pagebreak;
- function __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();
- }
-
- public function getpages()
- {
- $contentlen = strlen($this->content);
- $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;
- }
-
- $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;
- }
- }
-
- function getpage()
- {
-
- 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]);
- }
- }
-
- public function getpagenav()
- {
- if($this->page > 1)
- {
- $pagenav = "<a href='".$this->geturl()."=".($this->page-1)."' class='div'>上一页</a> ";
- }
-
- for($j=1;$j<=$this->pagecount;$j++)
- {
- if($j == $this->page)
- {
- $pagenav .= "<span class='divsi'>".$j."</span> ";
- }
- else
- {
- $pagenav .= "<a href='".$this->geturl()."=".$j."' class='div'>".$j."</a> ";
- }
- }
-
- if($this->page < $this->pagecount && $this->pagecount >1)
- {
- $pagenav .= "<a href='".$this->geturl()."=".($this->page+1)."' class='div'>下一页</a> ";
- }
- return $pagenav;
- }
-
- 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();
- ?>
(责任编辑:admin) |