i am doing this in all my Controllers for Menu But i want to do that i give it once in core folder of CI Any Suggestion ?

$data['cms_menus']=$this->database_model->GetRecords('cms_menus',false, array('FKMenuID'=>null));
foreach ($data['cms_menus'] as $key => $datas){
$data['cms_menus'][$key]['childs']=$this->database_model->GetRecords('cms_menus',false, array('FKMenuID'=> $datas['PKMenuID']));}

`

Dani AI

Generated

For : the pattern pointed to (prepare the menu once in a shared place instead of repeating it in every controller) is the right idea. Two practical refinements will make that approach faster and more robust.

First, avoid an N+1 query pattern (one query for parents then one per parent). Fetch all menu rows in a single query and build the nested structure in PHP. Example pattern (adapt field names to your schema):

$rows = $this->database_model->get_all_menus(); // flat rows with 'id' and 'parent_id'
$items = [];
foreach ($rows as $r) {
    $r['children'] = [];
    $items[$r['id']] = $r;
}
$tree = [];
foreach ($items as $id => &$item) {
    if ($item['parent_id'] && isset($items[$item['parent_id']])) {
        $items[$item['parent_id']]['children'][] = &$item;
    } else {
        $tree[] = &$item;
    }
}
unset($item);
// $tree is a nested menu you can cache or pass to views

Second, cache the result (file, Redis, memcached or CI cache driver) and invalidate the cache when menus change. Also: autoload or load the model in your base controller, avoid heavy DB work directly in constructors on high-traffic pages, and validate for circular parent links to prevent infinite loops. If you cannot change every controller, a hook (post_controller_constructor) can populate the menu via get_instance(), but be careful about what subsystems are available at that hook point. These steps give one-query efficiency plus safe reuse across the app.

Hello,

in ./application/core/ create the file MY_Controller.php and extend it to CI_Controller and add a public (or protected) $data:

class MY_Controller extends CI_Controller {

    public $data;
    public function __construct()
    {
        parent::__construct();
        $this->data['menu'] = 'MENU DATA HERE';
    }
}

Then extend your controllers to MY_Controller instead of CI_Controller and when you want to send data to the views use $this->data instead of the canonical $data, for example:

class Blog extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->data['title'] = 'Blog Index';

        $this->load->view('common/header', $this->data);
        $this->load->view('blog/index', $this->data);
        $this->load->view('common/footer', $this->data);
    }
}

And it should work. Due to the nature of CI you don't have a lot of flexibility, if you are still starting consider other solutions like SlimPHP & co.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.