Trying to get property of non-object codeigniter view

I tried foreach data from controller to my view, but I get error trying to get property of non-object in my foreach($menu_down as $menu_downs)


public function index() {
    $data['menu'] = $this->M_home->tampil_menu_utama();
    $data2 = array(); //initialize your variable with array; 

    foreach ($data['menu'] as $menus) {
        $idmenu = $menus->menuid;
        // echo $idmenu."<br>";
        // print_r($data['menu_down']); 
        $data2['menu_down'][] = $this->M_home->tampil_menu_down($idmenu);
        // print_r($data);
    // print_r($data['menu_down']);
    $this->load->view('head', array_merge($data, $data2));


function tampil_menu_down($idmenu){        
    $query = $this->db->query("SELECT menuname FROM `menu_master` WHERE root='".$idmenu."'");        
    return $query->result();


<?php foreach($menu as $menus){ ?>

    <li class="treeview">
      <a href="#"><i class="fa fa-link"></i>
        <span><?php echo $menus->menuname ?></span>
        <span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>
      <ul class="treeview-menu">

        <?php foreach($menu_down as $menu_downs){ //error ?>

          <li><a href="#"><?php echo $menu_downs->menuname ?></a></li>

        <?php } ?>


<?php } ?>

2 answers

  • answered 2018-01-19 03:33 Nikhil K

    Try to print the $menus in view and check whether it is object or array. If it is an array then you can not access it like $menus->menuname

    If possible please post the output here.

  • answered 2018-01-19 03:55 Alex

    You should not use array merge this way as it has unintended effects.

    Do this instead:

    public function index() {
        $menu = $this->M_home->tampil_menu_utama();
        $data = array();
        foreach ($menu as $menus) {
            $data[] = $this->M_home->tampil_menu_down($menus->menuid);
        $this->load->view('head', array('menu' => $menu, 'menu_down' => $data));

    If that fails try this:

        <?php foreach($menu_down as $k => $v){ //error ?>
          <li><a href="#"><?php echo $v->menuname ?></a></li>
        <?php } ?>