Example of tp5 of thinkPHP5 framework using DB to implement batch delete function

  • 2021-12-11 17:29:15
  • OfStack

This article illustrates how tp5 (thinkPHP5 framework) uses DB to implement batch deletion. Share it for your reference, as follows:

1-Front Page

html


<a href="javascript:;" rel="external nofollow" onclick="datadel()" class="btn btn-danger radius">
<i class="Hui-iconfont" >&#xe6e2;</i>  Bulk deletion </a>
<td><input type="checkbox" value="{$category.id}" name="id"></td>

js


function datadel(obj){
  layer.confirm(' Are you sure you want to bulk delete? ',function(index){
    var id=new Array();
    $('input[name="id"]:checked').each(function(){
      id.push($(this).val());// To add elements to an array 
    });
   $.post(
      "{:url('admin/article_category/delAllCategory')}",
      {id:id},
      function(dat){
        if(dat){
          $(obj).parents("tr").remove();
          layer.msg(' Deleted !',{icon:1,time:1000});
          document.location.reload();// Current page 
        }else{
          var msg = dat.msg;
          layer.msg(msg,{icon:2,time:2000});
        }
      });
  });
}

2-Controller


public function delAllCategory(){
    $id=input("id/a");
    // Method 1
    $id = implode(",",$id) ;
    $data=Db::name("article_categorys")->where("id in ($id)")->delete();
    // Method 2
     $data=Db::name("operation")->delete($id);
    exit(json_encode($data));
  }
//  Delete by primary key 
Db::table('think_user')->delete(1);
Db::table('think_user')->delete([1,2,3]);
//  Conditional deletion 
Db::table('think_user')->where('id',1)->delete();
Db::table('think_user')->where('id','<',10)->delete();

For more features and plug-ins, please refer to the official address: https://www.kancloud.cn/he_he/thinkphp5/787173

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: