My table [cat_id,title,pid]. I need to get all the child sub-categories ID`s in the following format:
[1] =>Array ( [cat_id] => 2 [title] => TEST [pid] => 1 ),
[2] =>Array ( [cat_id] => 3 [title] => TEST [pid] => 1 ),
[3] =>Array ( [cat_id] => 4 [title] => TEST [pid] => 2 ),
[4] =>Array ( [cat_id] => 5 [title] => TEST [pid] => 3 )
Purpose – using ID`s of the childs, to get all of these items.
I tried to do something as the following code, but it does not work:
public function get_ids($tree,$cid = 0)
{
$data = $this->db->select('cat_id, title, pid')
->where('pid',$cid)
->get('catalog');
$result = array_push($tree,$data->result_array());
if($data->num_rows() > 0){
foreach ($data->result_array() as $r) {
return $this->get_ids($result,$r['cat_id']);
}
}
else{
return $result;
}
}
Maybe there is better way?
Try the below code to get all child ids by given parent id.