When we do some website projects, we will encounter such a problem. Delete a column, and this column is not the bottom column. That is to say, the deleted column has sub-columns. At this time, we execute To delete the column command, you need to delete the column and its sub-columns together, because we cannot let the sub-column still be displayed or exist in the database after the superior column of a class is deleted. At this time, the problem of column deletion becomes coming.
First picture:
This is a simple permission management page. In the picture, the administrator permissions are top-level permissions, and the column management is administrator permissions. Sub-permissions of column addition are sub-permissions of column management. For such a three-level classification, the effect we want to achieve is: delete the administrator permission, and all the column management and column adding permissions under it will be deleted at the same time; delete the column management permission , the column addition will also be deleted at the same time; deleting the column addition permission will only delete the column itself.
Next implementation method:
1. It is a simple deletion method in the controller:
1 public function privilege_del(){2 $pri = D('privilege');3 $id = I('id');4 if($pri->delete($id)){5 $this->success('删除权限成功!',U('Privilege/privilege_lst'));6 }else{7 $this->error('删除权限失败!');8 }9 }
2. We think that the result of simultaneous deletion we want is actually that when we choose to delete the upper-level column, the code first helps us delete the bottom-level sub-column of the column category, and then deletes it upwards in order, and finally deletes what we selected. The superior column, so we need to write a pre-constructor to operate_before_delete($options) This is the function provided to us by ThinkPHP. Its usage is to execute this function before we execute the delete method, where $options is The information we want to delete is specifically a two-dimensional array. When we dump $options, the result is: , you only need to get $options['where']['id'], but note that if where in the array is also an array, this means that we are performing batch deletion, which is also a single deletion and The difference between batch deletion and batch deletion. Regarding batch deletion, I will write another article to briefly introduce it. This time we will only write about single deletion.
1 public function childid($priid){2 $data = $this->select();3 return $this->getchildid($data,$priid);4 }Then based on all Data search for the id of the sub-column below the column we want to delete, and create a method getchildid again to get all the ids that need to be deleted
1 public function getchildid($data,$parentid){ 2 static $ret=array(); 3 foreach ($data as $k => $v) { 4 if($v['parentid']==$parentid){ 5 $ret[]=$v['id']; 6 $this->getchildid($data,$v['id']); 7 } 8 } 9 return $ret;10 }Here again recursion is used to query in all data After completing the lower-level columns, query the lower-level columns of the lower-level columns, and finally save the id versions of all sub-columns of the column into the ret array and return. Finally call
1 public function _before_delete($options){2 //单个删除3 $chilrenids =$this->childid($options['where']['id']);4 $chilrenids = implode(',', $chilrenids); 5 if($chilrenids){6 $this->execute("delete from ed_privilege where id in($chilrenids)");7 }8 }in the constructor to separate the obtained ID numbers with commas, because the delete method can no longer be used to delete data at this time. If used, then Calling the constructor again will become an infinite loop, so we need to execute SQL statements here to delete data. The above is all the content deleted from a single column.
The above is the detailed content of How to delete a column?. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools