2013-04-09 51 views
0

我對codeigniter是相當新的,似乎無法得到這個問題的持有,總之我有問題了解CodeIgniter框架之間的數據流。所有我想要顯示的使用情態動詞,並在視圖中顯示從數據庫中獲取數據..如何保存和顯示值從模態到數據庫使用codeigniter查看

在我的控制器中的代碼是:

$unitData =$this->ClientUnit->getBlockUnits($client_block_ID); 
foreach ($unitData->result() as $row){ 
    for ($i=0; $i < 3; $i++) { 
    $client_unit_name[$i] = $row->client_unit_name[$i]; 
    $unit_owner_name[$i] = $row->unit_owner_name[$i];  
    } 
} 
$data['client_unit_name'] = $client_unit_name; 
$data['unit_owner_name'] = $unit_owner_name; 
$this->load->view('newblock_unit',$data); 

我的樣式文件是:

function getBlockUnits($client_block_ID) { 
    $query = $this->db->query('SELECT * FROM client_units where client_block_ID="'.$client_block_ID.'"'); 
    return $query; 
} 

由於你可以看到,從我的模態和控制器代碼,問題是因爲多行返回,目前我只得到所有輸出中的最後一行視圖,而不是單個行...

回答

0

我認爲問題在於你沒有返回一個對象或數組。在這個模型中,你應該使用,一個對象:

return $query->result(); 

或爲數組:

return $query->result_array(); 

編輯:您可以不考慮這個閱讀這裏的一切http://ellislab.com/codeigniter/user-guide/database/results.html

+0

你的答案几乎是正確的,但我已經得到了答案,你也重定向我的答案我一直在尋找,但因爲我是新來的StackOverflow,我不能回答只是尚未:P 實際我忘了提及在for循環中要更改的行,如您在以下提到的鏈接底部所述: $ row = $ unitData-> row($ i); – 2013-04-09 08:33:00

+0

@blogger如果你沒有把我的標記標記爲正確,那麼只需關閉線程或者對此做些什麼...... – jakobhans 2013-09-18 16:05:40

0

您可以發送整個陣列在視圖和循環它那裏

$unitData =$this->ClientUnit->getBlockUnits($client_block_ID); 


$data['unitData '] = $unitData->result_array(); 
$this->load->view('newblock_unit',$data); 

在您的視圖中使用循環獲得t他對應的是

foreach($unitData as $row){ 
    echo row['client_unit_name']; 
    echo row['unit_owner_name']; 
} 
相關問題