0
votes

I'm new to Codeigniter and i have been trying to develop some part using it. On my header file, i need to load my menu items and i have create a menu controller, menu model and a view. Controller page

<?php
  class Menu extends CI_Controller
  {
 public function __construct(){
     parent::__construct();
     $this->load->model('menu_model');

 }
 public function index(){

    $data['menuArray'] = $this->mainMenuDataLoad();

    if($data['menuArray']){
        $this->load->view('menu' , $data);
    }
 }

 public function mainMenuDataLoad(){ /* create menus Array */

    $rootMenuData = $this->menu_model->loadManuData();

    if($rootMenuData){
        for($e=0; $e<count($rootMenuData); $e++){
            if($rootMenuData[$e]){
                $data[$e] = array(
                        'title' => $rootMenuData[$e]['title'],
                        'menu_id' => $rootMenuData[$e]['menu_id'],
                        'url' => $rootMenuData[$e]['url'],
                        'menu_icon' => $rootMenuData[$e]['menu_icon'],
                );

                $get_sub = $this->mainMenuDataLoad($rootMenuData[$e]['menu_id']);
                if($get_sub){
                    $data[$e]['sub'] = $get_sub;
                }
            }
        }
        return $data;
    }
    return false;
 }   }

this is my model page

class Menu_model extends CI_Model{

public function loadManuData(){
    $this->db->select("*");
    $this->db->from('tbl_menu');
    $this->db->order_by("order", "DESC");
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        $r=0;
        foreach ($query->result() as $row) {
            $data[$r]['root_id'] = $row->root_id;
            $data[$r]['menu_id'] = $row->menu_id;
            $data[$r]['title'] = $row->title;
            $data[$r]['url'] = $row->url;
            $data[$r]['menu_icon'] = $row->menu_icon;
            $r++;
        }
        return $data;
    }
    return false;
}

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

andon my menu view page i am looping the menu data. But on my header.php if i try to call the menu controller like this

$this->load->controller('menu');

it gives me an error like this.

Fatal error: Call to undefined method CI_Loader::controller() on header.php

What am i doing wrong?. Someone please guide me. thanks in advance

menu.php view Page

 <ul class="nav navbar-nav">
    <?php 
    print_r($menuArray);
            for($q=1; $q<count($menuArray); $q++){ 

        ?><li>
                        <a href="<?php echo base_url($menuArray[$q]['url']);?>">
                            <span class="<?php echo $menuArray[$q]['menu_icon'];?>">
                            <?php echo $menuArray[$q]['title'];?>
                            </span>
                        </a>
                    </li>
        <?php   }

    ?>

  </ul>
2
Instead loading menus via controller try to use helpers. Just define your method in helper file, load it via autoload and call the helper method from view. By this way you will call a method in header file and will be available everywhere.k10gaurav

2 Answers

2
votes

you cant call a controller from view

i.e. in view page writing this code $this->load->controller('menu'); is not permissible.

The controller loads the view and model, its the controller that is the prime here

[More Edit:]

change your model to this

class Menu_model extends CI_Model{
public function __construct(){
    parent::__construct();
}

public function get_menu()
{
    $this->db->select("*");
    $this->db->from('tbl_menu');
    $this->db->order_by("order", "DESC");
    $query = $this->db->get();

    return $query;

}

}?>

then in the controller do this

public function index(){

            $data['menuArray'] = $this->menu_model->mainMenuDataLoad()->result_array();
            $this->load->view('header', $data);
            $this->load->view('menu'); // u are passing data from here//
            $this->load->view('landing_page');
            $this->load->view('footer');
         }

and finally the view

<?php
    if(count($menuArray)>0)
    {
    for($q=0; $q<count($menuArray); $q++){ 

        ?><li>
                        <a href="<?php echo base_url($menuArray[$q]['url']);?>">
                            <span class="<?php echo $menuArray[$q]['menu_icon'];?>">
                            <?php echo $menuArray[$q]['title'];?>
                            </span>
                        </a>
                    </li>
        <?php   }
    }?>

You also dont need the public function mainMenuDataLoad() function in controller

0
votes

Better solution if you make a BaseController with a function and call it from extended controllers.

BaseController:

class BaseController extends CI_Controller 
{
protected $data = array();

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

     $this->load->model('my_model');
}

protected function LoadContView($aContentView) {
    $this->data['menu'] = $this->my_model->getMenu();

    $this->load->view('common/ViHeader', $this->data);
    $this->load->view($aContentView, $this->data);
    $this->load->view('common/ViSidebar', $this->data);
    $this->load->view('common/ViFooter', $this->data);
}

Mypage:

class Mypage extends BaseController{

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

    public function index() {
        $this->LoadContView('my_view');
    }

}

also use foreach( $menu_array as $menu_item) :)