Thinkphp implements sorting and sticking to the top

【Foreword】

     The background needs to add sorting and sticking operations, so I thought of a solution to achieve it. Please correct me if there is any misunderstanding

 

【main body】

(1) Sorting operation ideas

Generally, they are sorted by release date. The timestamp is larger, so use the reverse order desc instead of asc

$model->order('addtime desc')->select();

 

(2) Top operation ideas:

When you click on the top, you can modify the database addtime field value to the current time. Because the sorting is by timestamp

<a href="__CONTROLLER__/sort/id/{$vol.id}">置顶</a>

public function sort(){
    $model = D('cate');
    $id = I('get.id');//Get click data id
    $addtime = time();//Current timestamp
    $cate->where('id='.$id)->setField('addtime',$addtime);//Update database timestamp
}

 

(3) At this time, you can set the sorting in the background, and then adjust it accordingly in the foreground controller

<?php
// public controller
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {
    public function __construct(){
        parent::__construct();
        //The following is automatically called for the public part
        $this->nav();
        $this->link();
        $this->news();
    }
    // top navigation
    public function nav(){
    	$ cate = D ('cate');
    	$data = $cate->order('addtime desc')->select();
    	$this->assign('data',$data);
    }
    //Link
    public function link(){
    	$link = D('link');
    	$linkdata = $link->order('addtime desc')->select();
    	$ this-> assign ('linkdata', $ linkdata);
    }
    //Recently published, just list a few in order by timestamp
    public function news(){
    	$link = D('article');
    	$newsdata = $link->order('addtime desc')->limit(10)->select();
        // dump($newsdata);
    	$this->assign('newsdata',$newsdata);
    }

}

 

 

 

 

.

Guess you like

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