2012-08-10 36 views
1

我試圖做使用我的模型foreach,但它給我一個錯誤並返回結果。做使用codeigntier查詢一個foreach?

下面的代碼:

public function read(){ 
     $questions; //array 
     $query = $this->db->get('questions'); 
     foreach($query->result() as $row){ 
      $getAnswers = $this->db->get_where('answers', array('question_id'=>$row['question_id]']), 4); 
      $questions= $getAnswers; 

      } 

     return $questions; 
    } 

,這是錯誤:

Fatal error: Cannot use object of type stdClass as array in

+0

什麼線是錯誤的? – 2012-08-10 14:33:09

+0

$ getAnswers = $這個-DB ......基本上5 – user1551482 2012-08-10 14:33:59

+0

我想在你的foreach的部分應該是$問題[] = $ getAnswers;否則,你只打算有最後一排......你也可以適當的的foreach .... $問題=陣列之前創建陣列(); ---只是我注意到 – 2012-08-10 14:44:25

回答

4

訪問row作爲一個對象:

$getAnswers = $this->db->get_where('answers', 
        array('question_id'=>$row->question_id), 4); 
+0

謝謝您的回答,這樣的作品,但我可以問你即時通訊返回問題對象數組正確的,因爲它從我 – user1551482 2012-08-10 14:37:22

+0

或者使用'$查詢 - > result_array說其錯誤感謝+1() '而不是'$ query-> result()'。 – Mischa 2012-08-10 14:37:25

+0

@ user1551482,這個答案修正錯誤,但您嘗試檢索的問題和答案的方式有許多缺陷。看看連接。 – Mischa 2012-08-10 14:39:04