2016-12-12 85 views
0

我有下面的代碼在我header.php$menu值從'my_controller.php &數據庫查詢來已menu_model.php定義。得到錯誤未定義指數笨

錯誤消息

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($菜單)

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" } } } 

有人可以幫助我找到錯誤?

+1

發表您的完整的錯誤消息 –

+0

通常未定義的索引錯誤也伴隨着行號,多到錯誤。你能發佈整個錯誤,還有一些行號嗎? – FrankerZ

+0

語義;未定義索引是通知 – zanderwar

回答

4

一些改變你的my_controller.php。而不是$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 
} 
+0

那太簡單了。我完成了錯過它,謝謝。:) –

+0

只做這個改變是不完整的解決方案。在header.php也有錯誤。 –

1

首先可以$data['menu'] = $menu['menu'];保存menu_model.phpMenu_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.phpMy_controller.php

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); 
} 

而在您的看法作如下代碼:

<?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; ?>