I want to delete record from mysql in hierarchical manner.
i have my database like this
id pid
1 0
2 1
3 2
i want to delete all above record. please reply with proper function in php mysql.
function content_deleted($content_id=0, $parent_id=0)
{
if($parent_id==0)
{
$query = $this->db->query("SELECT content_id FROM tbl_content WHERE content_id = $content_id");
}
else
{
$query = $this->db->query("SELECT content_id FROM tbl_content WHERE content_parent_id = $content_id");
}
if($query->num_rows()>0)
{
foreach($query->result() as $res)
{
$id = $res->content_id;
if($parent_id==0)
{
$this->db->query("DELETE FROM tbl_content WHERE content_id = $id");
}
else
{
$this->db->query("DELETE FROM tbl_content WHERE content_parent_id = $id");
$this->content_deleted($content_id, $id);
}
}
}
}
this is my function im stucked here it onnly delete one record
Relational databases do not lend themselves easily to tree structures. If you only have
idandparent_idfields, you’ll have to do a lot of looping and recursion to work with trees. That’s why Nested Sets or MPTT were invented. You should move to that model for your trees and this question will solve itself.