thinkphp5.0 文章详情页 上一篇 下一篇

 //    上一篇下一篇(同一个分类下,先确定该分类的pid)
    public function frontAfter()
    {
        $param=$this->param;
        $front=Db::name('news')->where(['pid'=>$param['pid']])->where('id','<',$param['id'])->field(['id','pid'])->order(['id'=>'desc'])->find();
        $after=Db::name('news')->where(['pid'=>$param['pid']])->where('id','>',$param['id'])->field(['id','pid'])->order(['id'=>'asc'])->find();
        $front=empty($front)?'没有了':$front;
        $after=empty($after)?'没有了':$after;
        $data=[$front,$after];
        return resultArray(['data'=>$data]);
    }

测试代码php部分:

 public function fenye()
    {
        $param=$this->param;
//        dump($param);
        $front=Db::name('news')->where(['pid'=>$param['pid']])->where('id','<',$param['id'])->order(['id'=>'desc'])->find();
        $after=Db::name('news')->where(['pid'=>$param['pid']])->where('id','>',$param['id'])->order(['id'=>'asc'])->find();
//        echo Db::name('news')->getLastSql();
//        dump($front);
//        dump($after);

        $f_url= $front['id'];
        $a_url=$after['id'];
        $front=empty($front)?'没有了':$front;
        $after=empty($after)?'没有了':$after;

        $this->assign('front',$front);
        $this->assign('after',$after);

        $this->assign('f_url',$f_url);
        $this->assign('a_url',$a_url);
        return $this->fetch('index');
    }

HTML部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>news 分页</title>
</head>
<body>
<div>分页</div>
{$front}&nbsp;&nbsp;&nbsp;--{$after}<br>
<a href="{$f_url}">上一页</a> &nbsp;&nbsp;&nbsp; <a href="{$a_url}">下一页</a>
</body>
</html>
原文地址:https://www.cnblogs.com/lxwphp/p/8243547.html