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