Codeigniter菜单未显示

时间:2016-06-29 12:19:19

标签: php codeigniter menu

我在网站上显示菜单问题。 链接website.com/menu显示数据库中的链接。

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

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

我不知道我做的方式是对还是不错......但它现在正在运作。

我已将此添加到application / core / 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);

     }

&安培;删除标题&amp; template.php文件中的菜单