thinkphp realizes the next article

【Foreword】

   This article introduces the grammar for realizing the upper and lower articles of articles or news, which can solve the problem of discontinuous IDs

 

【main body】

(1) Part 1

$pre = M('article')->where(array('id>"'.$_GET["id"].'"','cateid' => $cateid))
                   ->limit('1')->find();

    Note: Here I have added a condition in where, that is, classification. That is, it is in the same category as the current article. Here, multiple conditions are added in the form of an array array. If there is no category, you can remove the cateid condition 

 

(2) Part 2

$next = M('article')->where(array('id<"'.$_GET["id"].'"','cateid' => $cateid))
                    ->order('id desc')
                    ->limit('1')
                    ->find();

    Note: Similar to the principle of the previous article  

 

(3) No more -- judge whether there is a chapter or not

         Note: Here you can add a judgment in the controller and pass it to the template, or you can judge directly in the template.

         ① Judge in the controller

if(!$pre){
           $pre['title']='No more (this article is the first article of this category)';
        }
if(!$next){
           $next['title']='No more (this article is the end of the category)';
        }

          After the judgment is completed, it can be passed to the template

         ② Judge in the template

            After the output, I know that the $pre or $next variable output is an empty array when there is no previous article, so it can be judged in the template

<if condition="$next eq null">
    <a href="javascript:;" class="prompt-next">没有了!</a>
<else/>
    <a href="__CONTROLLER__/index/id/{$next.id}" title="{$next['title']|msubstr=###,0,40}">{$next['title']|msubstr=###,0,40}</a>
</if>

    Perfect, in order to improve the user experience, add a click event to the situation without context

<script type="text/javascript">
    $(function(){
        $('.prompt-pre').on('click',function(){
            alert("This article is the first article of this category");
        });
        $('.prompt-next').on('click',function(){
            alert("This article is the last article of this category");
        });
    })
</script>

 

 

 

 

 

 

 

 

 

..

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326173925&siteId=291194637