php CodeIgniter 将多个数组从控制器传递到视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14132290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:43:05  来源:igfitidea点击:

CodeIgniter passing multiple arrays from controller to view

phpcodeigniterundefined

提问by Michael F

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

可能重复:
PHP:“注意:未定义变量”和“注意:未定义索引”

I'm trying to pass both session data and a db query to a view in Codeigniter. I get an error in my view file that says: 'Message: Undefined variable: data'

我正在尝试将会话数据和 db 查询传递给 Codeigniter 中的视图。我在我的视图文件中收到一条错误消息:“消息:未定义变量:数据”

The problem lies with in the $idea variable. The variables register an undefined error.

问题在于 $idea 变量。变量注册一个未定义的错误。

Controller:

控制器:

public function members() {

    if($this->session->userdata('is_logged_in')){
        $session = $this->session->all_userdata();
        $facebook_id = $data['id']; 


        $this->load->model('idea'); 
        $idea = $this->idea->get_idea($facebook_id); 

        //merge the two arrays to get data variable
        $data = $session + $idea; 
        $this->load->view('members', $data); 
    } else {
        redirect('main/login'); 
    }
}

Model:

模型:

public function get_idea($facebook_id) {
    $data = array(
        'id' => '', 
        'idea' => '', 
        'facebook_id' => $facebook_id
    );

    $query = $this->db->get('idea', $facebook_id); 

    return $query->result_array(); 

}

Here are some of the resources I've used:
-Creating foreach loops using Code Igniter controller and view
-codeigniter: pass array from controller to view
-http://blog.mattheufarmer.com/2010/building-a-website-in-codeigniter-part-ii-getting-data-and-using-it/

以下是我使用过的一些资源:
-使用 Code Igniter 控制器和视图创建 foreach 循环
- codeigniter:将数组从控制器传递到视图
- http://blog.mattheufarmer.com/2010/building-a-website-in -codeigniter-part-ii-getting-data-and-using-it/

Thank you for the help!

感谢您的帮助!

回答by Josh Brody

$data = $session + $idea; 

No. Just no. That's not how you merge the arrays.

不就是不。这不是你合并数组的方式。

A cleaner, more thoughtful approach to do what you want to do:

一种更干净、更周到的方法来做你想做的事:

$data = array( $session, $idea );

Otherwise,

除此以外,

$data = array_merge($session, $idea);

Edit:

编辑:

You should read this http://ellislab.com/codeigniter/user-guide/general/views.html

你应该阅读这个http://ellislab.com/codeigniter/user-guide/general/views.html

Anyway, unless you're doing something completely stupid, you should be able to do this:

无论如何,除非你在做一些完全愚蠢的事情,否则你应该能够做到这一点:

$data = array( 'sesssion' => $sesssion, 'idea' => $idea ); 

And pass that through to your view. And then on your view, you can call the session with $session, and idea with $idea. To illustrate:

并将其传递给您的视图。然后在你看来,你可以用 $session 调用会话,用 $idea 调用idea。为了显示:

$data = array('name' => 'josh', 'sex' => 'often');
$this->load->view('welcome', $data);

And then on my view:

然后在我看来:

<? echo "$name has sex $sex"; // returns josh has sex often ?>

In the views, the keys become variables and the values are accessed by those variables (which were keys in $data). You don't call $data, you call the key.

在视图中,键成为变量,值由这些变量($data 中的键)访问。您不调用 $data,而是调用密钥。

$data = array('key' => 'value');

Let me know if you have any other issues. :)

如果您有任何其他问题,请告诉我。:)

回答by Suresh Kamrushi

You can do like this-

你可以这样做-

public function members() {

    if($this->session->userdata('is_logged_in')){
        $data['session'] = $this->session->all_userdata();
        $facebook_id = $data['id']; 


        $this->load->model('idea'); 
        $data['idea'] = $this->idea->get_idea($facebook_id); 

        $this->load->view('members', $data); 
    } else {
        redirect('main/login'); 
    }
}