编程语言
首页 > 编程语言> > thinkphp5实现上下分页

thinkphp5实现上下分页

作者:互联网

 思路,

从页面获取章节id即chapterid

通过chapterid查询数据库中的小说的id即novelid根据novelid查询小说对应的所有章节

上一页的写法思路

查询数据库中的章节表即 chapter 表 根据chapterid字段进行倒序查询,然后根据页面中获取到的chapterid和数据库中的chapterid比较,获取大于页面传来的chapterid的第一个字段

下一页思路和上面差不多

列如chapter表里面有3个数据,chapterid分别是1 2 3 默认页面为https:/....?id=2 

$pro的值就等于1 $next的值就等于3

后台代码如下,为了方便理解我并没有使用model

public function test(){

        从页面获取传来的章节id字段,同时从数据库中根据该字段查询对应的小说id字段
        $chapterid=input('id');
        $novelid=Db::name('chapter')->where('chapterid',$chapterid)->value('novelid');
        //上一页,获取小于传来的章节id的id字段
        $pro=Db::name('chapter')->where('novelid','=',$novelid)->where('chapterid','<',$chapterid)->order('chapterid desc')->limit('1')->value('chapterid');

        //下一页,获取大于传来的章节id的id字段
        $next=Db::name('chapter')->where('novelid','=',$novelid)->where('chapterid','>',$chapterid)->order('chapterid asc')->limit('1')->value('chapterid');

        $this->assign(array('pro'=>$pro));
        $this->assign(array('next'=>$next));
        return view();
    }

前台写法

 <div class="bottem2">
            {if condition = "($pro == '')"}
            <a>暂无上一章</a>
            {else}
             <a href="__URL__/novelChapterDetails?id={$pro}">上一章</a>
             {/if}
             {if condition = "($next == '')"}
             <a>已是最新章节</a>
             {else}
              <a href="__URL__/novelChapterDetails?id={$next}">下一章</a>
              {/if}
             <a href="__URL__/collection?id={$chapter.novelid}&&chapterid={$chapter.chapterid}">加入书签</a>
        </div>

 

标签:章节,分页,pro,thinkphp5,next,novelid,上下,chapterid,id
来源: https://blog.csdn.net/qq_40452641/article/details/117522400