在thinkphp开发的项目中,下面的方法可以轻松获取上一篇、下一篇文章,但是只适用于文章id必须是自增长的int型。
方法其实很简单,根据当前文章的分类id即cateid,选出所有同类型的文章,然后根据id进行降序排列,再选出比当前id小的一篇,即为上一篇文章,比当前id大的一篇,即为下一篇:
02 | public function getPrev( $aid , $cateid , $url ) |
04 | $prev = Db::table( 'article' )->field( 'id,title' )->where( 'cate_id' , $cateid )->where( "id < $aid" )->order( 'id desc' )->limit(1)->find(); |
05 | if (! empty ( $prev )) $prev [ 'url' ] = $url . $prev [ 'id' ]; |
10 | public function getNext( $aid , $cateid , $url ) |
12 | $next = Db::table( 'article' )->field( 'id,title' )->where( 'cate_id' , $cateid )->where( "id > $aid" )->order( 'id' )->limit(1)->find(); |
13 | if (! empty ( $next )) $next [ 'url' ] = $url . $next [ 'id' ]; |
使用方法:
1 | $prevArc = $this ->getPrev( $aid , $arcDetail [ 'cate_id' ], '/index/honor/' ); |
2 | $nextArc = $this ->getNext( $aid , $arcDetail [ 'cate_id' ], '/index/honor/' ); |
部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!