I want to delete category from multilevel menu with recursion function. To understand situation more carefully , lets take a look first at mysql table structure
CREATE TABLE IF NOT EXISTS menu(
id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
p_id INT(5),
sort_id INT(5) NOT NULL,
title VARCHAR(50) CHARSET utf8 COLLATE 'utf8_unicode_ci' NOT NULL,
);
it looks like this when it is retrieved
id | p_id | sort_id | title |
1 | 0 | 1 | root1 |
2 | 1 | 1 | sub of root1
3 | 0 | 2 | root2 |
4 | 2 | 1 | sub of "sub of root1"
... | ... | ... | ....
etc ...
I've written php script for delete category, here it is =>
function del_cat($connection,$id){
if (!$connection->connect_errno){
if ($connection->set_charset("utf8")){
if ($r = $connection->query("SELECT id FROM menu WHERE p_id=" . $id . "")){
if ($r->num_rows>0){
while ($row = $r->fetch_assoc()){
del_cat($connection,$row['id']);
}
} else {
$connection->query("DELETE FROM menu WHERE id=" . $id . "");
}
$r->free();
}
}
}
}
$connection
variable is a just mysql connection object, and $id
is id
in table.
It works just fine when I'm deleting one row ( I mean when category doesn't have a child , sub categories), for example 1 | 0 | 1 | root1 |
, but when I want to delete for example 4 | 2 | 1 | sub of "sub of root1"
it doesn't delete category with sub categories . Any idea how to solve this problem ? thanks