TP5 easily realizes previous article next article

TP5 easily realizes previous article next article

I’m working on a project that involves small functions like the previous one and the next one. There must be similar functional blocks on the Internet. I didn’t find Barabara. It was actually some trouble. I can’t write it myself and share it with everyone.

First of all, there are some things to explain about the column of the previous article and the next one. Now there are three columns, top-level column 1 and two second-level columns 2 and second-level column 3. They can all publish articles, but top-level columns When you click the display, the content in the sub-column is usually displayed, so there are two situations. In the first case, you are directly accessing an article from the second-level column. The previous and next articles corresponding to the article must be searched from the current column. If you don't have it, you will be prompted to find it. Situation 2: If you are accessing the article from the top-level column, then I think you can’t just search from the column to which this article belongs, but from these three columns. Of course, if you find it troublesome You can also find only the articles in the column to which the current article belongs.

Here is my method:

Methods written in the class:

public function index($aid)

{

	$arts=db('article')->find($aid);//当前文章所有信息

	$cid=$arts['cate_id'];//当前栏目id

	$soncids=model('cate')->getChildIds($cid);//这个方法是自己写的,根据当前栏目的id查找所有子栏目的id

	$soncids[]=intval($cid);//获取包含当前栏目在内的子栏目的id数组

	$prev=db('article')->where('cate_id','in',$soncids)->where('id','<',$aid)->limit(1)->find();//上一篇文章

	$next=db('article')->where('cate_id','in',$soncids)->where('id','>',$aid)->limit(1)->find();//下一篇文章

	$this->assign([

		'arts'=>$arts,

		'prev'=>$prev,

		'next'=>$next,

		]);

    return view('newsdetail');

}

Application of template layer:

<div class="prev col-lg-6 col-md-12 col-sm-12 clo-xs-12">

	{if condition="$prev"}

	<a href="{:url('Article/index',array('aid'=>$prev['id']))}">上一篇:{$prev.title}</a>

	{else /}

	上一篇:暂时没有了~

	{/if}

</div>

<div class="next col-lg-6 col-md-12 col-sm-12 clo-xs-12">

	{if condition="$next"}

	<a href="{:url('Article/index',array('aid'=>$next['id']))}">下一篇:{$next.title}</a>

	{else /}

	下一篇:暂时没有了~

	{/if}

</div>

Guess you like

Origin blog.csdn.net/weixin_45557228/article/details/109574470