Home > Article > Backend Development > How to delete a column in ThinkPHP?
I published an essay about deleting columns some time ago. The function implemented at that time was to delete a piece of information. This time I will implement batch deletion of columns.
What we need to achieve is the following effect:
After selecting the batch delete button, you can select all the columns of the page. This It is the implementation of the front-end page. I won’t go into details here. Let’s go directly to the topic: the batch deletion function.
1. There is also a small point here on the front-end page, that is, the name value of the selection box should be assigned an array of ids, and the value value is the id of the column.
2. We write a separate batch deletion method in the controller
1 public function privilege_bdel(){ 2 $ids = I('ids'); 3 $pri = D('privilege'); 4 $ids = implode(',', $ids); 5 if($ids){ 6 if($pri->delete($ids)){ 7 $this->success('批量删除栏目成功!',U('Privilege/privilege_lst')); 8 }else{ 9 $this->error('批量删除栏目失败,请重试!');10 }11 }else{12 $this->error('未选中任何内容,请重试!');13 }14 }
Here we need to pass the id array Convert to a comma-separated string such as: 1,2,3, so that the delete operation can be performed directly.
This is the result of our dump options. This is also the basis for us to judge whether the deletion is a single deletion or a batch deletion. If options[where][id] If it is an array, it means that batch deletion is being performed, otherwise it is a single deletion.
3. Modification of the constructor before deletion in the model
1 public function _before_delete($options){ 2 //批量删除 3 if(is_array($options['where']['id'])){ 4 $arr = explode(',', $options['where']['id'][1]); 5 $sonpri = array(); 6 foreach ($arr as $k => $v) { 7 $sonpri2 = $this->childid($v); 8 $sonpri = array_merge($sonpri,$sonpri2); 9 }10 $sonpri = array_unique($sonpri);11 $chilrenids = implode(',', $sonpri);12 }else{//单个删除13 $chilrenids =$this->childid($options['where']['id']);14 $chilrenids = implode(',', $chilrenids);15 }16 if($chilrenids){17 $this->execute("delete from ed_privilege where id in($chilrenids)");18 }19 }
Here we write the code for batch deletion (single deletion has been written before, no longer Tips)
Convert the passed string into an array without commas and store it in $arr. Create an empty array $sonpri, and then traverse foreach. Here, first find out all the sub-column IDs (will use childid function), store it in the $sonpri2 array, then merge $sonpri and $sonpri2 into an array, use the array_merge() function to complete, so that we can obtain the ids of all sub-columns, but in the id group we obtained , there will be many duplicate IDs, so here we also need to perform a duplication operation, using the array_unique() function. Finally, split the array again to get the id string, then delete it and you're done.
The above is the detailed content of How to delete a column in ThinkPHP?. For more information, please follow other related articles on the PHP Chinese website!