Recently made a brief answer to the article details page, you need to add to the bottom of the page, the Next button, analysis, the most basic need to have the title and ID of the article (as a parameter).
Start thinking is the current ID plus minus 1, but considering that if the partial ID is missing is wrong, so the separate query is larger than the current ID and small record and limit to one, so the following code.
The code below, the wrong place please advise.
In the controller
Query on-Article $prev _article = your model:: Find () ->andfilterwhere ([' < ', ' ID ', $id]) ->andfilterwhere ([ Other conditions) ->orderby ([' id ' = Sort_desc]) ->limit (1) ->one (); Query Under-article $next _article = your model:: Find () ->andfilterwhere ([' > ', ' id ', $id]) ->andfilterwhere ( Other conditions) ->orderby ([' id ' = SORT_ASC]) ->limit (1) ->one (); $model [' prev_article '] = [ ' url ' = =!is_null ($prev _article)? Url::current ([' id ' = $prev _article->id]): ' javascript:; ', ' title ' =!is_null ($prev _article)? $prev _ Article->title: ' No ', ]; $model [' next_article '] = [ ' url ' = =!is_null ($next _article)? Url::current ([' id ' = $next _article->id]): ' javascript:; ', ' title ' =!is_null ($next _article)? $next _ Article->title: ' No ', ]; return $this->render (' View ', ' model ' = $model, );
In the View
<div class= "Left" > <p> prev: <a href= "<?= $model [' prev_article '] [' url ']?> ' > <?= $model [' prev_article '] [' title ']?> </a> </p> </div> <div class= ' Right "> <p> Next: <a href=" <?= $model [' next_article '] [' url ']?> ' > <?= $model [' Next_article ' [' title ']?> </a> </p> </div>
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.