2013-04-12 67 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;