thinkphp实现上一篇与下一篇的功能(不是你想的那么简单)

实现文章上一篇与下一篇的功能;
大家可能觉得难度不大;
百度上一搜基本上都是一种答案;
白俊遥博客

但是他有个缺陷;
就是如果id不是自增呢?
而是由项目需求而自定义生成的一串长数字呢?

所以当id这个自增条件破坏了之后;
上图这个方法便不行了;
我是这样实现的;
我以created_at(数据创建时间)为顺序获得上一篇与下一篇的id;


<span style="color: #6089b4;">$id</span> <span style="color: #676867;">=</span> <span style="color: #ce6700;">I</span>(<span style="color: #9aa83a;">'get.id'</span>);
<span style="color: #6089b4;">$created_at</span> <span style="color: #676867;">=</span> <span style="color: #6089b4;">$<span style="color: rgb(96, 137, 180);">Article</span></span><span style="color: #676867;">-></span><span style="color: #ce6700;">getFieldById</span>(<span style="color: #6089b4;">$id</span>,<span style="color: #9aa83a;">"created_at"</span>);
<span style="color: #9a9b99;">// 获取上一篇id</span>
<span style="color: #6089b4;">$up_where</span>\[<span style="color: #9aa83a;">'created_at'</span>\] <span style="color: #676867;">=</span> <span style="color: #9872a2;">array</span>(<span style="color: #9aa83a;">'ELT'</span>,<span style="color: #6089b4;">$created_at</span>);
<span style="color: #6089b4;">$<span style="color: rgb(96, 137, 180);">up_where</span></span>\[<span style="color: #9aa83a;">'id'</span>\] <span style="color: #676867;">=</span> <span style="color: #9872a2;">array</span>(<span style="color: #9aa83a;">'NEQ'</span>,<span style="color: #6089b4;">$id</span>);
<span style="color: #6089b4;">$up_id</span> <span style="color: #676867;">=</span> <span style="color: #6089b4;">$Article</span><span style="color: #676867;">-></span><span style="color: #ce6700;">where</span>(<span style="color: #6089b4;">$<span style="color: rgb(96, 137, 180);">up_where</span></span>)<span style="color: #676867;">-></span><span style="color: #ce6700;">order</span>(<span style="color: #9aa83a;">"created_at desc"</span>)<span style="color: #676867;">-></span><span style="color: #ce6700;">getField</span>(<span style="color: #9aa83a;">'id'</span>);
<span style="color: #9a9b99;">// 获取下一篇id</span>
<span style="color: #6089b4;">$down_where</span>\[<span style="color: #9aa83a;">'created_at'</span>\] <span style="color: #676867;">=</span> <span style="color: #9872a2;">array</span>(<span style="color: #9aa83a;">'EGT'</span>,<span style="color: #6089b4;">$created_at</span>);
<span style="color: #6089b4;">$<span style="color: rgb(96, 137, 180);">down_where</span></span>\[<span style="color: #9aa83a;">'id'</span>\] <span style="color: #676867;">=</span> <span style="color: #9872a2;">array</span>(<span style="color: #9aa83a;">'NEQ'</span>,<span style="color: #6089b4;">$id</span>);
<span style="color: #6089b4;">$down_id</span> <span style="color: #676867;">=</span> <span style="color: #6089b4;">$Article</span><span style="color: #676867;">-></span><span style="color: #ce6700;">where</span>(<span style="color: #6089b4;">$<span style="color: rgb(96, 137, 180);">down_where</span></span>)<span style="color: #676867;">-></span><span style="color: #ce6700;">order</span>(<span style="color: #9aa83a;">"created_at desc"</span>)<span style="color: #676867;">-></span><span style="color: #ce6700;">getField</span>(<span style="color: #9aa83a;">'id'</span>);

获取到两个id之后;
传到前台当作get参数传递即可;
前台view代码:


<span style="color: #6089b4;"><</span><span style="color: #ff0b00;">empty</span><span style="color: #9872a2;"> </span><span style="color: #d0b344;">name</span><span style="color: #9872a2;">=</span><span style="color: #9aa83a;">"up_id"</span><span style="color: #6089b4;">></span>
没有了
<span style="color: #6089b4;"><</span><span style="color: #ff0b00;">else</span><span style="color: #6089b4;"> /></span>
<span style="color: #6089b4;"><a</span><span style="color: #d0b344;"> href=</span><span style="color: #9aa83a;">"__ACTION__/id/{$up_id}"</span><span style="color: #6089b4;">></span>
上一篇
<span style="color: #6089b4;"></a></span>
<span style="color: #6089b4;"></</span><span style="color: #ff0b00;">empty</span><span style="color: #6089b4;">></span>
<span style="color: #6089b4;"></span>


<span style="color: rgb(96, 137, 180);"><</span><span style="color: rgb(255, 11, 0);">empty</span><span style="color: rgb(152, 114, 162);"> </span><span style="color: rgb(208, 179, 68);">name</span><span style="color: rgb(152, 114, 162);">=</span><span style="color: rgb(154, 168, 58);">"down_id"</span><span style="color: rgb(96, 137, 180);">></span>


没有了


<span style="color: rgb(96, 137, 180);"><</span><span style="color: rgb(255, 11, 0);">else</span><span style="color: rgb(96, 137, 180);"> /></span>


<span style="color: rgb(96, 137, 180);"><a</span><span style="color: rgb(208, 179, 68);"> href=</span><span style="color: rgb(154, 168, 58);">"__ACTION__/id/{$<span style="color: rgb(154, 168, 58);">down_id</span>}"</span><span style="color: rgb(96, 137, 180);">></span>


下一篇


<span style="color: rgb(96, 137, 180);"></a></span>


<span style="color: rgb(96, 137, 180);"></</span><span style="color: rgb(255, 11, 0);">empty</span><span style="color: rgb(96, 137, 180);">></span>

这样就可以不受id约束了;

木点点 i
请先登录后发表评论
  • 最新评论
  • 总共1条评论
木点点 i

安稳:以后以为学长没有个人博客,如今发现做的这么好,值得好好学习!

2018-09-06 09:43:04 回复

木点点 i
  • 木 点点 i 回复 安稳:相互学习!
  • 2018-09-06 19:36:14 回复