2016-08-08 33 views
1

我有在控制器「thisTopic」的函數模型返回的陣列讀取的特定字段,其具有以下:如何從由控制器本身

$viewdata = array(); 
$viewdata['reply'] = $this->disc->get_all_replies($topic_id); 
$viewdata['firstReply']= $this->disc->get_firstReply(); 

在模型:

public function get_all_replies($topic_id){ 

    return $results = $this->db->select('*') 
        ->from('disc_replies') 
        ->join('users', 'users.id = disc_replies.users_id') 
        ->where('topic_id',$topic_id) 
        ->get()->result(); 
} 

public function get_firstReply($reply_id){ 

    return $results = $this->db->select('*') 
        ->from('disc_firstReply') 
        ->join('users', 'users.id = disc_firstReply.users_id') 
        ->where('reply_id',$reply_id) 
        ->get()->result(); 
} 

disc_replies表有以下幾列:

1. reply_id 
2. topic_id 
3. users_id 
4. replied_on 

$viewdata['reply']我想讀取第一行的reply_id字段,並將其作爲參數發送給get_firstReply()函數。

這是我試過到目前爲止:

$viewdata['firstReply']= $this->disc->get_firstReply($viewdata['reply'][0]['reply_id']); 

$viewdata['firstReply']= $this->disc->get_firstReply($viewdata['reply']['reply_id']); 

但沒有任何工程,任何幫助解決這個問題真的讚賞。在此先感謝

回答

0

爲了在codeigniter中使用此$viewdata['reply'][0](0指向數組),您必須將數組格式設置爲目標數組。

->get()->result(); 

應該改變爲

->get()->result_array(); 

您可以使用此print_r($viewdata['reply'])


檢查數組的格式

確保$viewdata['reply']不爲空