简体   繁体   English

使用Codeigniter访问多维数组

[英]Accessing Multidimensional array with Codeigniter

Ok, In my Codeigniter project I am passing a multidimensional array to my view. 好的,在我的Codeigniter项目中,我正在将多维数组传递给视图。 The issue I am having is access the data in the array. 我遇到的问题是访问数组中的数据。 I use print_r and var_dump to see the array and it is being passed correctly to the view but I am having the hardest time access the data within it! 我使用print_r和var_dump来查看数组,并将其正确传递给视图,但是我最困难的时间访问其中的数据! I get this error message, "Trying to access parameter of non-object". 我收到此错误消息,“试图访问非对象的参数”。 Any suggestions?! 有什么建议么?!

Here is the controller: profile.php 这是控制器:profile.php

    <?php
        class Profile extends CI_Controller {

            public function __construct(){
                parent::__construct();
                $this->load->library('session');

                //Get user data
                $this->load->model('user_model');

            }

            public function user_lookup(){
                //get usering users data
                $email = $this->session->userdata('email');
                //get profile users data
                $username = $this->uri->segment(2,0);

                $user = array(
                            'users' => $this->user_model->getUserData($email),
                            'profile' => $this->user_model->getUserDataWithUsername($username)
                        );

                $this->load->view('profile_view', $user);
            }

        }
    ?>

And here is the view that is recieving the data: profile_view.php 这是接收数据的视图:profile_view.php

<!DOCTYPE html>
<html>
    <body>
        <?php
             print_r($users[0]);
        ?>
     </body>
 </html>

The output of my print_r statement is: 我的print_r语句的输出是:

Array ( [hometown] => Las Vegas [email] => johnmy@gmail.com [university] => UC Berkley [first_name] => Pete [last_name] => Smith [date] => 1992 ) 1 Array([家乡] =>拉斯维加斯[电子邮件] => johnmy@gmail.com [大学] =>加州大学伯克利分校[first_name] => Pete [last_name] => Smith [date] => 1992)1

Based on your code: 根据您的代码:

$this->load->view('profile_view', $user);

This mean that you supply array $user as view data. 这意味着您将提供数组$user作为视图数据。 Therefore, on view $user is no longer exists. 因此,在视图上$user不再存在。

You can only use it's element in view: 您只能在视图中使用它的元素:

<?php
    echo $hometown;
    echo $email;
?>

If you want to use $user array in view, use this view load code in controller: 如果要在视图中使用$user数组,请在控制器中使用此视图加载代码:

$this->load->view('profile_view', array('user' => $user ));

You can read more about Views in CodeIgniter . 您可以在CodeIgniter中阅读有关视图的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM