Facing some issues in multi level category hierarchy Facing some issues in multi level category hierarchy codeigniter codeigniter

Facing some issues in multi level category hierarchy


The problem is that your nesting code is referencing nonexistent IDs in $array.

For "tin rasugulla", parent_id = 3, which doesn't exist on the root level of $array so it gets created, when it should in fact try to find the parent with ID 3, which is below "sweets".

This should work:

public function get_nested(){    // fetching categories from table    $this->db->order_by($this->_order_by);    $pages = $this->db->get($this->_table_name)->result_array();    // now creating category tree    foreach ($pages as $page){        if ($page['parent_id'] == 0){            $array[$page['id']] = $page;        } elseif (isset($array[$page['parent_id']])) {            $array[$page['parent_id']]['children'][$page['id']] = $page;        } else {            foreach ($array as $root_id => $parent) {                if (isset($parent['children'][$page['parent_id']])) {                    $array[$root_id]['children'][$page['parent_id']]['children'][$page['id']] = $page;                }            }        }    }    return $array;}