2016-06-29 131 views
1

我在網站上顯示菜單問題。 鏈接website.com/menu顯示來自數據庫的鏈接。Codeigniter菜單不顯示

enter image description here

但在網站上它沒有顯示的菜單欄。 enter image description here

控制器菜單:

public function index() 
{  
    $this->load->model("menu_model"); 
    $data = array(); 

    if ($menu_query = $this -> menu_model-> getCategories()) { 
     $data['recordsmenu'] = $menu_query; 
    } 
    $this->load->view("includes/menu", $data); 
} 

Menu_model:

public function getCategories() 
{ 
    $this->db->select('*'); 
    $this->db->from('category_name'); 
    $this->db->where('parent_id','0'); 
    $this->db->order_by('category_id', 'asc'); 
    $menu_query = $this->db->get(); 

    if ($menu_query->num_rows() != 0) { 
     return $menu_query->result(); 
    } else { 
     return false; 
    } 
} 

菜單視圖:

<ul class="nav navbar-nav"> 
    <?php if(isset($recordsmenu)) : foreach ($recordsmenu as $menu): ?>   
    <li><a href="<?php echo base_url(); echo $menu->linkname;?>"><?php echo $menu->catname;?></a></li> 
    <?php endforeach; ?> 
    <?php else : ?> 
    <?php endif; ?> 
</ul> 

雖然網站的所有其他頁面的控制器是:

function index() 
{ 
    $this->load->model('slides_model'); 
    if ($query = $this -> slides_model-> get_records()) { 
     $data['records'] = $query; 
    } 
    $data['main_content'] = 'home'; 
    $this ->load->view('includes/template', $data); 
} 

template.php文件中的包括文件夾是:

<?php $this->load->view('includes/header'); ?> 
<?php $this->load->view('includes/menu'); ?> 
<?php $this->load->view($main_content); ?> 
<?php $this->load->view('includes/footer'); ?> 
+1

請告訴我錯誤或輸出將此添加到應用程序/核心/ MY_Controller.php

function __construct() { parent::__construct(); $this->load->model("menu_model"); $data = array(); if($menu_query = $this -> menu_model-> getCategories()) { $data['recordsmenu'] = $menu_query; } $this-> load -> view('includes/header'); $this->load->view("includes/menu", $data); } 

&去除報頭&菜單?? –

+0

您需要製作全局變量並在template.php中傳遞菜單數據。目前template.php沒有收到任何數據。 – krutssss

+0

'recordsmenu'和'records'是兩個不同的數據變量 –

回答

0

我不知道我做的方式是對還是不是......但現在工作。

我已經從template.php文件中

+0

不!你不要在覈心文件夾中這樣做。你從不這樣做。你所做的就是複製粘貼我答案的第一部分,並在'/ application/core /'文件夾中創建一個名爲MY_Loader.php的文件。而我的答案的第二部分是你在你的controller @ user3901675中所做的 – frank

0

目前您的菜單視圖接收不到任何數據。一個有效的解決方案是,你可以添加以下內容application/core/MY_Loader.php

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class MY_Loader extends CI_loader{ 

    public function view_loader($main_content_view, $dat = array(), $return=FALSE) 
    { 
     if($return==TRUE): 
      $content = $this->view('partials/header_view', $dat, $return); 
      $content .= $this->view('partials/menu_view', $dat, $return); 
      $content .= $this->view($main_content_view, $dat, $return); 
      $content .= $this->view('partials/footer_view', $dat, $return); 

      return $content; 
     else: 
      $this->view('partials/header_view', $dat); 
      $this->view('partials/menu_view', $dat); 
      $this->view($main_content_view, $dat); 
      $this->view('partials/footer_view', $dat); 
     endif; 
    } 

,並用它在你的控制器加載意見這種方式:

public function method_name() 
{ 
    if ($menu_query = $this -> menu_model-> getCategories()) { 
    $data['recordsmenu'] = $menu_query; 
    } 
    $data['some_other_data'] = $this->model_name->method_name(); 
    $this->load->view_loader("main_content_view_name", $data); 
} 
+0

我也試過這個,但它不幫幫我。 –

+0

你可以發佈你做了什麼? @ user3901675 – frank

+0

是啊當然...只是加上答案 –