使用Codeigniter访问多维数组 [英] Accessing Multidimensional array with Codeigniter
问题描述
好的,在我的Codeigniter项目中,我将多维数组传递给视图。我遇到的问题是访问数组中的数据。我使用print_r和var_dump来查看数组,并将其正确传递给视图,但是我很难访问其中的数据!我收到此错误消息,试图访问非对象的参数。有什么建议吗?!
Ok, In my Codeigniter project I am passing a multidimensional array to my view. 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! I get this error message, "Trying to access parameter of non-object". Any suggestions?!
这里是控制器:profile.php
Here is the controller: 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);
}
}
?>
这是接收数据的视图:profile_view.php
And here is the view that is recieving the data: profile_view.php
<!DOCTYPE html>
<html>
<body>
<?php
print_r($users[0]);
?>
</body>
</html>
我的print_r语句的输出为:
The output of my print_r statement is:
Array([hometown] => Las Vegas [email] => johnmy@gmail.com [university] => UC Berkley [first_name] => Pete [last_name] => Smith [date] => 1992)1
Array ( [hometown] => Las Vegas [email] => johnmy@gmail.com [university] => UC Berkley [first_name] => Pete [last_name] => Smith [date] => 1992 ) 1
推荐答案
根据您的代码:
$this->load->view('profile_view', $user);
这意味着您提供了数组 $ user
作为查看数据。因此,视图 $ user
不再存在。
This mean that you supply array $user
as view data. Therefore, on view $user
is no longer exists.
您只能在视图中使用它的元素:
You can only use it's element in view:
<?php
echo $hometown;
echo $email;
?>
如果要使用 $ user
数组在视图中,请在控制器中使用以下视图加载代码:
If you want to use $user
array in view, use this view load code in controller:
$this->load->view('profile_view', array('user' => $user ));
您可以阅读有关在CodeIgniter中查看。
这篇关于使用Codeigniter访问多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!