0

I have below code in my header.php, values of $menu is coming from 'my_controller.php & database query has been defined in menu_model.php.

Error Message

Severity: Notice
Message: Undefined Index: menu_name
Filename: Header.php
Line number: 187
Backtrace:
    File: ////Header.php
    Line: 187
    Function: _error_handler

    File: ////my_controller.php
    Line: 32
    Function: view

menu_model.php

public function get_main_menu()
{
    $this->db->select("*");
    $this->db->from('menu');
    $this->db->order_by("menu_id", "ASC");
    $query = $this->db->get();
    return $query;
}

my_controller.php

protected function loadHeader($title = 'Home')
{       
    $menu['menu'] = $this->menu_model->get_main_menu()->result_array();
    //var_dump($menu['menu']); ***This shows data which is mentioned below
    $data['menu'] = $menu;
    $data['title'] = $title;
    $this->load->view('templates/header', $data);  ***This is line 32
}

header.php

<?php foreach ((array_values($menu)) as $mega_menu): ?> 
    <li class="mega-menu">
    <a href="<?php echo BASE_URL.('/category/'.$mega_menu['menu_id'].'/'); ?>">
    <div><?php echo $mega_menu['menu_name']?></div>
    </a> ***This is line 187
    </li>
<?php endforeach; ?>

var_dump($menu)

array(1) { ["menu"]=> array(4) { [0]=> array(4) { ["menu_id"]=> string(1) "1"
["menu_name"]=> string(14) "Start Business" ["menu_desc"]=> string(34) "DSC,
DPIN Initiation etc. services" ["breadcrumb_id"]=> string(1) "0" } [1]=> array(4)
{ ["menu_id"]=> string(1) "2" ["menu_name"]=> string(12) "Intellectual" ["menu_desc"]=>
string(35) "Trademark and Copyright Filing etc." ["breadcrumb_id"]=> string(1) "0" }
[2]=> array(4) { ["menu_id"]=> string(1) "3" ["menu_name"]=> string(8) "Personal"
["menu_desc"]=> string(47) "Wills, Rent Agreements, General Affidavits etc."
["breadcrumb_id"]=> string(1) "0" } [3]=> array(4) { ["menu_id"]=> string(1) "4"
["menu_name"]=> string(9) "Contracts" ["menu_desc"]=> string(32) "General
Business Agreements etc." ["breadcrumb_id"]=> string(1) "0" } } }

Can somebody help me finding the error?

9
  • 1
    post your complete error message Commented Dec 12, 2016 at 4:00
  • Usually undefined index errors are also accompanied by a line number, and more to the error. Can you post the entire error, along with some line numbers? Commented Dec 12, 2016 at 4:00
  • Semantics; Undefined Index is a Notice Commented Dec 12, 2016 at 4:02
  • @AbdullaNilam I have added the error message. Commented Dec 12, 2016 at 4:09
  • 1
    that's not error fix. its just tip. Commented Dec 12, 2016 at 4:18

2 Answers 2

4

Some change your my_controller.php. Can $data['menu'] = $menu['menu']; instead of $data['menu'] = $menu;

protected function loadHeader($title = 'Home')
{       
    $menu['menu'] = $this->menu_model->get_main_menu()->result_array();
    //var_dump($menu['menu']); ***This shows data which is mentioned below
    $data['menu'] = $menu['menu'];
    $data['title'] = $title;
    $this->load->view('templates/header', $data);  ***This is line 32
}
Sign up to request clarification or add additional context in comments.

2 Comments

That was so simple. I completed missed it, thanks.:)
making only this changes is not complete solution.There is also error in header.php.
1

First save menu_model.php as Menu_model.php with following Code

public function get_main_menu()
{
    $this->db->select("*");
    $this->db->from('menu');
    $this->db->order_by("menu_id", "ASC");
    $query = $this->db->get();
    return $query;
}

Save my_controller.php as My_controller.php with following code.

protected function loadHeader($title = 'Home')
{       
    $this->load->helper('url');
    $this->load->model('menu_model');
    $menu= $this->menu_model->get_main_menu()->result_array();
    $data['menu'] = $menu;
    $data['title'] = $title;
    $this->load->view('templates/header', $data);
}

And at your view make the following code:

<?php foreach($menu as $mega_menu): ?> 
    <li class="mega-menu">
    <a href="<?php echo base_url('category/'.$mega_menu['menu_id']); ?>">
    <div><?php echo $mega_menu['menu_name'];?></div>
    </a>
    </li>
<?php endforeach; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.