2016-10-03 88 views
2

而不是調用控制器中的每個函數內的相同的數據,我想調用一次全局加載數據。 我做如何在所有頁面上加載相同的內容?

MY_Controller.php 
<?php 

class MY_Controller extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url','file')); 
     $this->load->model('common/common_model'); 
     $data['header_menus'] = $this->common_model->categoryMenus(); 
    } 
} 

這個文件的核心文件夾中,並在控制器文件夾中有一個控制器和我做

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

class Home extends MY_Controller { 

而家居控制器內裝載功能如何加載$數據[「header_menus」 ]在視圖中,我是否需要再次在函數中使用該變量進行一些操作?

+0

你不使用佈局?請檢查http://stackoverflow.com/questions/19976737/how-to-create-master-pagelayout-with-base-design-style – madankundu

+0

把所有這一切都放在'視圖助手',並從佈局調用此助手 –

回答

1

創建視圖文件夾一個視圖,並加載在控制器視圖,使您的控制器的變化:

例如,您的視圖名稱是display_view.php在查看文件夾:

MY_Controller.php

<?php 

class MY_Controller extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url','file')); 
     $this->load->model('common/common_model'); 
     $data['header_menus'] = $this->common_model->categoryMenus(); 
     $this->load->view('tour/view',$data); 
    } 
} 

display_view.php

<?php 
if(!empty($header_menus)) { 
    extract($header_menus); 
} 
print_r($header_menus); // you can get all the info here 
?> 

Load View in Codeigniter

0

檢查關於繼承。

$data['header_menus'] = $this->common_model->categoryMenus(); 

必須

$this->data['header_menus'] = $this->common_model->categoryMenus(); 

在擴展的控制器需要用

$this->data['header_menus']; 

使用$this->data陣列其他變量也稱它爲並調用視圖的東西,如:

$this->load->view('some_view_file', $this->data); 

請參閱文檔herehere,here,herehere

相關問題