thinkphp How to delete columns?

Source: Internet
Author: User
Some time ago published a delete column of the essay, at that time realized the function is to delete a message, this time to realize the bulk delete column.

What we need to achieve is such an effect:

Select the Bulk Delete button can select all the columns of this page, this is the implementation of the front page, here is not much to say, we go directly to the topic: bulk deletion of the function.

1. Front page There is also a small point here, that is, the name value of the selection box should give him an array with an ID assigned to it, the value of the column is the ID.

2. We write a method of bulk deletion 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 (' Bulk Delete column succeeded! ', U (' privilege/privilege_lst ')); 8             }else{9                 $this->error (' Bulk Delete column failed, please try again! '),             }11         }else{12             $this->error (' No content selected, please try again! ');         }14     }

Here to convert the passed ID array into a comma-delimited string such as:----------

This is the result of our dump out options, which is also our use to determine whether the deletion is a single delete or bulk deletion, if OPTIONS[WHERE][ID] is an array, then the bulk of the deletion, otherwise it is a single delete.

3. Modifications to the pre-delete constructor in the model

1 public Function _before_delete ($options) {2         //Bulk Delete 3         if (Is_array ($options [' where '] [' ID '])) {4             $arr = Explode (', ', $options [' where '] [' ID '][1]); 5             $sonpri = Array (), 6             foreach ($arr as $k = = $v) {7                 $sonpri 2 = $this->childid ($v); 8                 $sonpri = arr Ay_merge ($sonpri, $sonpri 2); 9             }10             $sonpri = Array_unique ($SONPRI), one             $chilrenids = Implode (', ', $sonpri);         }else{//Single Delete 13             $chilrenids = $this->childid ($options [' where '] [' ID ']),             $chilrenids = Implode (', ', $chilrenids); 15         }16         if ($chilrenids) {             $this->execute ("Delete from Ed_privilege where ID in ($chilrenids)"); 18         }19     }

Here we write the bulk delete code (a single delete has been written before, no longer mentioned)

Convert the passed string into an array form without commas in the $arr, create an empty array $sonpri, and then foreach traversal, here first find out all the sub-column ID (will use the childID function), in the $sonpri2 array, and then $sonpri and $ Sonpri2 is merged into an array, which is done using the Array_merge () function, so we get the ID of all the sub-columns, but there are many duplicate IDs in the ID group we get, so here we're going to do the deduplication, using the Array_unique ( function Finally, the array is split again, the ID string is obtained, and then the deletion is done.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.