Recursively delete a node and all nodes under that node sample _php tutorial

Source: Internet
Author: User
Sometimes when you delete information, you need to delete all of this information, this time you need to delete the recursive. The following is a section of code that I wrote when I was doing the deletion of departmental functions in department management by recursively deleting a section and all of its sub-departments. For everyone reference and themselves later spare.

Here is a demonstration of my code:
Copy CodeThe code is as follows:
/*
* Modify a Department information
*/
Function del ($bumen _id) {
$sql = "Select bumen_id from Lxsm_bumen where topbumen_id=". $bumen _id; Query for bumen_id with topbumen_id as $bumen_id
$delsql = "Delete from lxsm_bumen where bumen_id=". $bumen _id; Remove departmental information bumen_id for $bumen_id
$xiaji _id= $this->db->fetch_assoc ($sql);
if ($xiaji _id) {
foreach ($xiaji _id as $id) {
$res = $this->del ($id [bumen_id]);
}
}
$result = $this->db->query ($delsql);
if ($result) {
return true;
}
else{
return false;
}
}

Note: The topbumen_id here is the ID number of the parent department in the department information, and the FETCH_ASSOC () function is a function that is already encapsulated, returning all the contents of the query to an array.

Experience: This is the first time to write a recursive algorithm, writing is still very immature, regardless of the function is realized. I feel that when writing recursive aspects of the Code, I first draw a tree-like structure, recognize its structure, according to the effect I want to get, first in the brain to simulate step by step execution. For example, the deletion here, want to delete a department, you have to delete themselves and find out what the sub-Department of the Superior department, the Ministry of the Department of one by one to traverse to get, when the operation of the Sub-department, and its parent department delete, need to delete themselves and find its sub-departments, In this way, there is a repeat operation, the operations of the Sub-department and the steps of the parent department are the same, so let it execute this function in the sub-department's traversal. In this way, a recursive algorithm is formed.

http://www.bkjia.com/PHPjc/743577.html www.bkjia.com true http://www.bkjia.com/PHPjc/743577.html techarticle Sometimes when you delete information, you need to delete all of this information, this time you need to delete the recursive. Here's what I did when I was doing department management's Delete department function ...

  • 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.