方法其实很简单,根据当前文章的分类id即cateid,选出所有同类型的文章,然后根据id进行降序排列,再选出比当前id小的一篇,即为上一篇文章,比当前id大的一篇,即为下一篇:
//获取上一篇 public function getPrev($aid, $cateid, $url) { $prev = Db::table('article')->field('id,title')->where('cate_id',$cateid)->where("id < $aid")->order('id desc')->limit(1)->find(); if (!empty($prev)) $prev['url'] = $url.$prev['id']; return $prev; } //获取下一篇 public function getNext($aid, $cateid, $url) { $next = Db::table('article')->field('id,title')->where('cate_id',$cateid)->where("id > $aid")->order('id')->limit(1)->find(); if (!empty($next)) $next['url'] = $url.$next['id']; return $next; }使用方法:
$prevArc = $this->getPrev($aid, $arcDetail['cate_id'], '/index/honor/'); $nextArc = $this->getNext($aid, $arcDetail['cate_id'], '/index/honor/');