【问题标题】:how to pass data from controller in multiple view如何在多个视图中从控制器传递数据
【发布时间】:2014-06-09 21:12:47
【问题描述】:

我想要的是在codeigniter的多个视图中从控制器传递数据,例如

function main(){
    $data['test'] = 'test';
    $json['json'] = $data
    $this->load->view('template/ajax', $json);
}

但除了“template/ajax”之外,我还想在 $this->load->view('home', $json) 中加载该数据,我/已经尝试过此代码,但它不起作用。

function main(){ 
    $data['test'] = 'test';
    $data['otherview'] = $this->load->view('home', $data);
    $json['json'] = $data
    $this->load->view('template/ajax', $json);
}

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    不确定这是否能解决您的问题,但这是我使用数组的方式:

    public $test = array();
    
    function  __construct() {
        parent::__construct();
        $this->variable['testing'] = "Testing Content";
    }
    
    function main() {
       $this->load->view('home', var_dump($this->variable));
    }
    
    function secondView {
        $this->load->view('template/ajax', var_dump($this->variable));
    }
    

    然后在你的视图文件中回显一个定义的 $variable。

    【讨论】:

      【解决方案2】:

      尝试:

      $data['otherview'] = $this->load->view('home', $data, true);
      

      参见 CI 手册:returning views as data

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多