Thinkphp 根据分页符实现文章分页

有时我们内容也内容太多太长,需要对内容也进行分页,所以百度了下相关的资料,测试下顺便记录下来

//内容页
public function detail(){
    //新闻内容start
    $pagecode = "/<pagebreak>(.*?)<\/pagebreak>/is";
    $pagestart = I('request.p')==0?1:I('request.p');
    $map['id'] =I("request.id");
    $map['published'] = 1;
    $arc = D('News')->get_one($map);
    if(!$arc){
        $this->_empty();
        exit;
    }
    $arc['content'] = htmlspecialchars_decode($arc['content']);
    $content_array = preg_split($pagecode,$arc['content']);
    $counts = count($content_array);
    if($content_array[0]){
        $count = $counts;
        $pagestart = $pagestart-1;
    }else{
        $count = $counts-1;    
    }
    //  echo  $pagestart;
    $p = new \Think\Page($count,1);  // 实例化分页类 传入总记录数和每页显示的记录数(25)
    //$p->url = "/^news\/(\d+)\_(\d+)$/";
    $p->setConfig('header','<span class="rows">共 %TOTAL_ROW% 页</span>');
    $p->setConfig('prev',"上一页");
    $p->setConfig('next','下一页');
    $p->setConfig('first','首页');
    $p->setConfig('last','最后一页');
    $p->setConfig('theme','  %up_page%  %link_page%  %down_page%  %end%');
    $p->search =  false;
    $p->url = '/news/'.I("request.id")."_";
    //var_dump($p);
    $show= $p->show();// 分页显示输出
    if($count==1){
        $this->assign("content",$content_array[0]);
    }else{
        $this->assign("content",$content_array[$pagestart]);
    }
    $this->assign("arc_page",$show);
    $this->assign("count",$count);
    $this->assign("arc",$arc);
}

<pagebreak>这个为分页符标识
需要开启分页路由

'/^news\/(\d+)$/'          =>  'news/detail?id=:1',

同时修改下page.class.php
page.class.php共享在附件中

Page.class.zip


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/451.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。