簡體   English   中英

如何使用Codeigniter保存和顯示模式值以通過數據庫查看

[英]How to save and display values from modal to view by database using 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; 
}

如您所見,從我的模態和控制器代碼來看,問題是由於返回了多行,目前我在視圖的所有輸出中僅獲得最后一行,而不是單個行...

我認為問題在於您沒有返回對象或數組。 在模型中,應使用以下對象:

return $query->result();

或對於數組:

return $query->result_array();

編輯:您可以在此處閱讀有關此內容的所有信息http://ellislab.com/codeigniter/user-guide/database/results.html

您可以在視圖中發送整個數組並在那里循環

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


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

在您的視圖中使用循環來獲取相應的

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM