上移/下移/置顶/批量删除/至尾

原文链接

原理部分:

前提:在数据库设置一个sort,根据数据库数据的条数自增,不可重复。

上移:从前台获取排序字段(我把它定为sort);根据sort,找到要交换的数据的id备用;然后sort+1,找到相邻的上一条数据,取得他的id;然后进行更新操作,吧要交换的数据的sort变成刚刚查出来的上一条数据的sort也就是前面的(sort+1),然后把上一条数据的sort,变成从前台获取的sort;

下移 :与上移思路相反

置顶:和sort==1的数据进行交换sort;

至尾:计算出数据表总共有多少条数据,这个总数结束最后一条数据的sort,然后进行交换。

删除:吧所有大于sort的数据获取出来,然后进行遍历,一一给他们sort减一

批量删除更新排序:吧传过来的数据变成一个数组,重复删除即可。

代码:让sort自动增长

public function ComnewsAdd(){
if(request()->isPost()){
$data=input(‘post.’);
$data[‘time’]=strtotime(date(‘YⓂ️d H:s:m’));

            //添加排序(重要部分)
            $count=db('comnews')->count();
            if($count){
                $data['sort']=$count+1;
            }else{
                $data['sort']=1;
            }

            $res=db('comnews')->insert($data);
            if($res){
                $this->success('添加新闻成功','ComnewsLis');
            }else{
                $this->error('添加新闻失败');
            }

        }

        return view('News/ComnewsAdd');
    }

只贴一个上移操作:

//向上移动
public function upsort(){
$data=input(‘post.’);
s o r t = sort= sort=data[‘pai’];//要修改的sort
m y d a t e = d b ( ′ c o m n e w s ′ ) − > w h e r e ( ′ s o r t ′ , ′ = ′ , mydate=db('comnews')->where('sort','=', mydate=db(comnews)>where(sort,=,sort)->find();
i d = id= id=mydate[‘id’];
//上一条数据
t o p o n e = topone= topone=sort+1;
t o p o n e r e s = d b ( ′ c o m n e w s ′ ) − > w h e r e ( ′ s o r t ′ , ′ = ′ , toponeres=db('comnews')->where('sort','=', toponeres=db(comnews)>where(sort,=,topone)->find();
if($toponeres){
//取出上一条数据的id和sort
t o p o n e i d = toponeid= toponeid=toponeres[‘id’];
t o p o n e s o r t = toponesort= toponesort=toponeres[‘sort’];

        //进行更新
        db('comnews')->where('id',$id)->setField('sort',$toponesort);
        db('comnews')->where('id',$toponeid)->setField('sort',$sort);

    }else{
        echo "<script>alert('已经在最顶上');</script>";

    }
    $this->redirect('/zxhl/public/admin/comnews/comnewslis.html');

}

批量删除:

public function del_all(){
d a t a = i n p u t ( ′ p o s t . ′ ) ; i f ( data=input('post.'); if( data=input(post.);if(data!=‘null’){
i d s = i m p l o d e ( ′ , ′ , ids=implode(',', ids=implode(,,data[‘checkbox’]);//对id进行接收

        $ids=explode(",",$ids);//放入数组中进行方便遍历
        for($i=0;$i<count($ids);$i++){
            //自身的sort
            $thesort=db('comnews')->field('sort')->where('id',$ids[$i])->find();
      $sortres=db('comnews')->field('id,sort')->where('sort','>',$thesort['sort'])->select();
            foreach($sortres as $key=>$value){
            //减一
        db('comnews')->where('id',$value['id'])->setField('sort',$value['sort']-1);
            }
        }
        if(db('comnews')->delete($ids)){
            $this->success('删除新闻成功','ComnewsLis');
        }else{
            $this->error('删除新闻失败');

        }
    }else{
        $this->error('未选中任何数据');

   }
}

效果图:

删除前

批量删除

删除后

——————————————

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值