一个网站建设爱好者,技术交流,相互学习
当前位置:首页>>程序猿
thinkphp5实现文章上一篇,下一篇
发布时间:2021-10-25 14:57:07 栏目:程序猿 阅读量:1472 作者:webcms

写在控制器:

//列表是按照根据id降序排列的,所以上一篇
        $prv=Db::table('web_article')->where('id','>',$id)->where('typeid',$typeid)->
order('id desc')->find();
        $this->assign('prv',$prv);
 //列表是按照根据id降序排列的,所以下一篇
        $next=Db::table('web_article')->where('id','<',$id)->where('typeid',$typeid)->
order('id asc')->find();
        $this->assign('next',$next);

我的完整版

 /**
     * 上一篇
     */
       public  function  pre($id="",$typeid=""){
           //列表是按照根据id降序排列的,所以上一篇
            $where=[
                 'id'=>['<',$id],
                 'typeid'=>$typeid
            ];
            $archives=new  art();
            $prv=$archives->ArticleFind($where,'id,title','id desc');
             return $prv;
       }

    /**
     * 下一篇
     */
       public  function  next($id="",$typeid=""){
            $where=[
                'id'=>['>',$id],
                'typeid'=>$typeid
            ];
            //列表是按照根据id升序排列的,所以下一篇
             $archives=new art();
             $next = $archives->ArticleFind($where,'id,title','id asc');

            return $next ;
        }

然后在文章方法里面调用上面两个方法

说明:$ares['id'] 表示文章的id,$ares['typeid']文章所属栏目id

$pre=$this->pre($ares['id'],$ares['typeid']);
 $next= $this->next($ares['id'],$ares['typeid']);
        if($pre){
            $article["pre"]="".$pre['title']."";
        } else{
            $article["pre"]="没有了";
        }
        if($next){
            $article["next"]="".$next['title']."";
        } else{
            $article["next"]="没有了";
        }

写在模板

上一篇:{$article.pre}

评论:

IT视频教程
Copyright © web小工匠 保留所有权利. 渝ICP备15008469号 网站地图