将变量传递给视图CodeIgniter时失败

时间:2014-05-29 16:21:02

标签: php codeigniter

我试图将$ variable从CodeIgniter控制器传递给视图,但它失败了。

我的控制器代码是:

class Index_controller extends CI_Controller {
    public function index(){
        $formURL = base_url().index_page().'authorization_controller/authorization';
        $attributes = array('class' => 'authorization','id' => 'authorization');
        $this->load->view('header');
        $this->load->view('leftFrame', $formURL, $attributes);
        $this->load->view('rightFrame');
        $this->load->view('index');
        $this->load->view('footer');
    }
}

我的观看代码是:

<?php echo form_open($formURL, $attributes); ?>
</form>

错误讯息:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: formURL

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: attributes

2 个答案:

答案 0 :(得分:0)

您需要传递数组或对象中的数据。然后,您可以通过引用与视图中的数组键同名的变量来访问此数据。你可以这样做:

public function index()
{
    $formURL = base_url().index_page().'authorization_controller/authorization';
    $attributes = array('class' => 'authorization','id' => 'authorization');

    // Data to pass to the view
    $view_data['formURL'] = $formURL;
    $view_data['attributes'] = $attributes;

    $this->load->view('header');
    $this->load->view('leftFrame', $view_data);
    $this->load->view('rightFrame');
    $this->load->view('index');
    $this->load->view('footer');
}

答案 1 :(得分:0)

您不需要将要发送到视图的数据分开。您需要使用CodeIgniters魔术数据阵列功能来执行此操作。

<强> CI Docs 滚动到标有&#34;将动态数据添加到视图&#34;

的部分

尝试将您的代码更改为以下内容......

class Index_controller extends CI_Controller {
    public function index(){
        $formURL = base_url().index_page().'authorization_controller/authorization';
        $attributes = array('class' => 'authorization','id' => 'authorization');

        //ADD Your variables to CodeIgniter Data Array
        $data["formURL"] = $formURL;
        $data["attributes"] = $attributes;

        $this->load->view('header');
        $this->load->view('leftFrame', $data);
        $this->load->view('rightFrame');
        $this->load->view('index');
        $this->load->view('footer');
    }
}