php上页下页,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;
}
PHP之友评论