2013-04-12 91 views
0

我有以下的情况发生在我的模型:访问结果数组中的笨

public function load_user_menu($username) 
    { 
     $this->db->select('*'); 
     $this->db->from('menu'); 
     $this->db->where('username', $username); 
     $query = $this->db->get(); 
     return $query->result(); 
    } 

,并在我的控制器如下:

public function index() 
    {  
     /*If user previously logged in and not logged out, username remains in session. 
      load username and load profile data. 
     */ 
     //check if user logged in or not 
     if(($this->session->userdata('username')!="")) 
     { 
      //load data from model 
      $profile = array(); 
      $username = $this->session->userdata('username'); 
      $result = $this->profileModel->user_profile($username); 
      foreach($result as &$value) 
       { 
        $profile['userdetails'] = $value; 
       } 
      $this->load->view('profile', $profile); 
     }else{ 
      //redirect to login function 
      $this->login(); 
     } 
    } 

,但我得到的曲线图的错误。我相信我访问他们错了,因为我做这个的观点:

<? echo $userdetails['profilepic']; ?> 

,并没有什么出,但这个错误:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/profile.php

Line Number: 60

这是因为错误的接入的肯定。我如何根据上述内容访问细节?

+0

哪里是'$的UserDetails [ 'profilepic'];'在你的代码?该错误表示没有任何东西可以从该变量中获得,并且没有任何东西进入'$ userdetails ['profilepic'];'中。 – MahanGM

+0

那么错误中提到的代码是什么? –

回答

3

assumming你有profilepic内的UserDetails和看到的错误,你试图让非对象的他财产,这意味着

<? echo $userdetails['profilepic']; ?> 

应该

<? echo $userdetails->profilepic; ?> 

,因为你正在服用你造成的对象,不阵列

return $query->result(); 

,或者您更改为

return $query->result_array(); 
+1

+1 http://ellislab.com/codeigniter/user-guide/database/results.html –

+0

谢谢...... :) :) – bipen

1

$query->result();产生一个对象; $query->result_array();给你一个数组

要么是好的,根据您的需要

+0

我认为这是我的答案说..:)... .. – bipen

+0

我想我们不需要评论和编辑时,两个人同时打字;发生很多 – jmadsen

+0

y我同意... :) :) ..干杯 – bipen

0

只是试试这个视图页面

<? echo $profilepic; ?> 
0

-there上是在您的控制器不需要foreach循环作为

$result = $this->profileModel->user_profile($username); 
      foreach($result as &$value) 
       { 
        $profile['userdetails'] = $value; 
       } 

- 试试这个。

$profile['userdetails'] = $this->profileModel->user_profile($username); 

- 要访问的视图,以此作为

echo $userdetails->profilepic;