php上页下页

PHP后台不在,开始边学边码php,倒腾了好久,功能实现了还没来得及开心,突然又发现另外一个简单快捷的解决方式,论熟读语法的重要性!!!!!!

老旧冗长啰嗦版

//下一条信息
        $nextid = intval($id)+1;
        $ndetail = clothes::find($nextid);
        //上一条信息
        $preid = intval($id) - 1;
        $predetail = clothes::find($preid);

        $lastId = intval(DB::table('clothes')->orderBy('id', 'desc')->first()->id);
        $firstId = intval(DB::table('clothes')->orderBy('id', 'asc')->first()->id);
       
        if(($nextid>=$lastId) && empty($ndetail)){
            $nextid = $id;
        } else {
            while (true) {
                 if (!empty(clothes::find($nextid))) {
                     $nextid = $nextid;
                     break;
                }
                $nextid += 1;
            }
        }
        if (($preid <= $firstId) && empty($predetail)) {
            $preid = $id;
        } else {
             while (true) {
                 if (!empty(clothes::find($preid))) {
                     $preid = $preid;
                     break;
                 }
                 $preid += 1;
             }
        }

简单快捷粗暴版

        $maxId = DB::table('clothes')->max('id');
        $minId = DB::table('clothes')->min('id');
        $preid = DB::table('clothes')->where('id', '<', $id)->max('id');
        $nextid = DB::table('clothes')->where('id', '>', $id)->min('id');
        if ($id == $maxId) {
            $nextid = $id;
            $preid = $preid;
        }
        if ($id == $minId) {
            $nextid = $nextid;
            $preid = $id;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值