2017-04-08 30 views
1

有人請幫助我,我用mvp程序使用CodeIgniter。我有一個這樣的數據庫:該表將一列中的數據顯示在codeigniter中的數據行中?

enter image description here

是可能的,如果我想在這樣的表視圖中顯示的數據?

enter image description here

在id_status一切都沒有工作的另一日期檢索數據時,我只能告訴你約會。

只有一個日期的控制器。

public function status() 
{ 
    $tanggal1 = '2017-03-28'; 
    $data['status'] = $this->M_Reports->status($tanggal1); 
    $this->load->view('homepage/template'); 
    $this->load->view('report/status', $data); 
} 

型號:

public function status($tanggal1) { 
     $sql = "SELECT operasional.id_ops, operasional.id_status, operasional.tanggal, peralatan.nm_peralatan FROM operasional INNER JOIN peralatan ON operasional.id_peralatan = peralatan.id_peralatan WHERE tanggal='$tanggal1' ORDER BY id_ops"; 
     return $this->db->query($sql)->result(); 
} 

,並查看:

<?php 
    $start = 0; 
    foreach($status as $data) { 
?> 
    <tr> 
    <td><?php echo ++$start ?> </td> 
    <td><?php echo $data->nm_peralatan ?></td> 
    <td><?php echo $data->id_status ?></td> 
    <td><?php echo $data->tanggal ?></td> 
    </tr> 
<?php 
    } 
?> 
+0

爲什麼不,你的觀點是基於你從你的數據庫中得到的。你只需要創建一個查詢來從你的數據庫表中獲取所需的索引。 –

+0

是您的視圖中的日期是靜態還是動態? –

+0

@georoot當我必須在不同日期時循環id_status時,我感到困惑。幫我 – khoirul

回答

0

您模型函數應該是這樣的

public function status($top_of_date) 
{ 
    // This will get all dates 
    $dates=$this->db->distinct()->select('tanggal')->from('operasional')->get()->result_array(); 
    // This will get all patients/peralatan 
    $patients=$this->db->distinct()->select('id_peralatan')->from('operasional')->get()->result_array(); 
    // Loop through peralatan 
    for($i=0;$i<count($patients);$i++) 
    { 
     $st=$this->select('id_peralatan')->from('operasional')->WHERE('id_peralatan',$patients[$i])->get()->result_array(); 
     // Start Creating an array for this index 
     $data[$i]=array(
      'id_peralatan'=>$st[0] 
     ); 
     // loop through dates 
     for($j=0;$j<count($dates);$j++) 
     { 
      //if date is greater than top of date 
      if($dates[$i]>$top_of_date) 
      { 
       $st=$this->db->select('id_status')->from('operasional')->WHERE('tanggal',$dates[$i])->get()->result_array(); 
       // Create index and save the status 
       $data[$i]['top_of_date']=$st[0]['id_status']; 
      } 
      elseif($dates[$i]<$top_of_date) 
      { 
       $st=$this->db->select('id_status')->from('operasional')->WHERE('tanggal',$dates[$i])->get()->result_array(); 
       // Create index and save the status 
       $data[$i][$dates[$i]]=$st[0]['id_status']; 
      } 
     } 
    } 
    return $data; 
} 

現在從你的控制器,你只需要發送頂部的日期。喜歡;

public function status() 
{ 
    $tanggal_top = '2017-03-28'; 
    $data['status'] = $this->M_Reports->status($tanggal_top); 
    $this->load->view('homepage/template'); 
    $this->load->view('report/status', $data); 
} 
+0

謝謝先生。 @Malik,我會試試這個;) – khoirul

+0

親愛的先生。 @Malik Mudassar,我從$('operasional') - > WHERE('id_peralatan',$ patients [$ i])有$ st = $ this-> db-> select('id_peralatan') - > >獲得() - > result_array();因爲$ patiensts仍然是數組,所以像這樣的錯誤SELECT'id_peralatan' FROM'operasional' WHERE'id_peralatan' ='Array' – khoirul

+0

如果您需要長時間的討論。請加入聊天http://chat.stackoverflow.com/rooms/11/php與我 –

相關問題