douju5062 2014-08-28 12:01
浏览 80
已采纳

使用codeigniter在头文件上加载菜单

I'm new to Codeigniter and i have been trying to develop some part using it. On my header file, i need to load my menu items and i have create a menu controller, menu model and a view. Controller page

<?php
  class Menu extends CI_Controller
  {
 public function __construct(){
     parent::__construct();
     $this->load->model('menu_model');

 }
 public function index(){

    $data['menuArray'] = $this->mainMenuDataLoad();

    if($data['menuArray']){
        $this->load->view('menu' , $data);
    }
 }

 public function mainMenuDataLoad(){ /* create menus Array */

    $rootMenuData = $this->menu_model->loadManuData();

    if($rootMenuData){
        for($e=0; $e<count($rootMenuData); $e++){
            if($rootMenuData[$e]){
                $data[$e] = array(
                        'title' => $rootMenuData[$e]['title'],
                        'menu_id' => $rootMenuData[$e]['menu_id'],
                        'url' => $rootMenuData[$e]['url'],
                        'menu_icon' => $rootMenuData[$e]['menu_icon'],
                );

                $get_sub = $this->mainMenuDataLoad($rootMenuData[$e]['menu_id']);
                if($get_sub){
                    $data[$e]['sub'] = $get_sub;
                }
            }
        }
        return $data;
    }
    return false;
 }   }

this is my model page

class Menu_model extends CI_Model{

public function loadManuData(){
    $this->db->select("*");
    $this->db->from('tbl_menu');
    $this->db->order_by("order", "DESC");
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        $r=0;
        foreach ($query->result() as $row) {
            $data[$r]['root_id'] = $row->root_id;
            $data[$r]['menu_id'] = $row->menu_id;
            $data[$r]['title'] = $row->title;
            $data[$r]['url'] = $row->url;
            $data[$r]['menu_icon'] = $row->menu_icon;
            $r++;
        }
        return $data;
    }
    return false;
}

public function __construct(){
    parent::__construct();
}}

andon my menu view page i am looping the menu data. But on my header.php if i try to call the menu controller like this

$this->load->controller('menu');

it gives me an error like this.

Fatal error: Call to undefined method CI_Loader::controller() on header.php

What am i doing wrong?. Someone please guide me. thanks in advance

menu.php view Page

 <ul class="nav navbar-nav">
    <?php 
    print_r($menuArray);
            for($q=1; $q<count($menuArray); $q++){ 

        ?><li>
                        <a href="<?php echo base_url($menuArray[$q]['url']);?>">
                            <span class="<?php echo $menuArray[$q]['menu_icon'];?>">
                            <?php echo $menuArray[$q]['title'];?>
                            </span>
                        </a>
                    </li>
        <?php   }

    ?>

  </ul>
  • 写回答

2条回答 默认 最新

  • drap5081683 2014-08-28 12:10
    关注

    you cant call a controller from view

    i.e. in view page writing this code $this->load->controller('menu'); is not permissible.

    The controller loads the view and model, its the controller that is the prime here

    [More Edit:]

    change your model to this

    class Menu_model extends CI_Model{
    public function __construct(){
        parent::__construct();
    }
    
    public function get_menu()
    {
        $this->db->select("*");
        $this->db->from('tbl_menu');
        $this->db->order_by("order", "DESC");
        $query = $this->db->get();
    
        return $query;
    
    }
    
    }?>
    

    then in the controller do this

    public function index(){
    
                $data['menuArray'] = $this->menu_model->mainMenuDataLoad()->result_array();
                $this->load->view('header', $data);
                $this->load->view('menu'); // u are passing data from here//
                $this->load->view('landing_page');
                $this->load->view('footer');
             }
    

    and finally the view

    <?php
        if(count($menuArray)>0)
        {
        for($q=0; $q<count($menuArray); $q++){ 
    
            ?><li>
                            <a href="<?php echo base_url($menuArray[$q]['url']);?>">
                                <span class="<?php echo $menuArray[$q]['menu_icon'];?>">
                                <?php echo $menuArray[$q]['title'];?>
                                </span>
                            </a>
                        </li>
            <?php   }
        }?>
    

    You also dont need the public function mainMenuDataLoad() function in controller

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名