2016-12-06 62 views
0

從數據庫中獲取的記錄得到錯誤這是我的代碼
同時使用codeiginter

user_controller.php

    public function get_users(){ 

        //load the database 
        $this->load->database(); 

        //load the model 
        $this->load->model('user_model'); 

        //load the method of model 
        $this->user_model->select(); 
        return //the data in view 
        $this->load->view('user_view', $data); 

        } 

user_model.php

  function select(){ 
     $query = $this->db->select('barcodeout','barcodein','emp_id','emp_name','amount','time'); 
     $this->db->from('porters'); 
     $query = $this->db->get(); 
     return $query->result(); 
} 

user_view.php

  <table> 
      <tr> 
      <td><strong>Ticket Out</strong></td> 
      <td><strong>Ticket In</strong></td> 
      <td><strong>Emp ID</strong></td> 
      <td><strong>Emp Name</strong></td> 
      <td><strong>Amount</strong></td> 
      <td><strong>Time</strong></td></tr> 

      <?php foreach($query->result() as $employee){ ?> 
      <tr> 
      <td><?=$employee->barcodeout;?></td> 
      <td><?=$employee->barcodein;?></td> 
      <td><?=$employee->emp_id;?></td> 
      <td><?=$employee->emp_name;?></td> 
      <td><?=$employee->amount;?></td> 
      <td><?=$employee->time;?></td> 
      </tr>  
       <?php }?> 
      </table> 
     </div> 
     </div> 

我已經用這個試了很多東西,我得到的最後是'未定義的變量',建議我用三個MVC代碼來獲取數據庫記錄,它會真的很感激,謝謝。

+1

發佈你正在得到的確切錯誤 –

+0

作爲一個方面說明,在你的控制器中,你不需要在$ this-> load-> view之前使用「return」。只需加載視圖和$數據。您還應該爲模型返回使用條件語句。如果($ this-> user_model-> select()){$ data ['employees'] = $ query-> result()} – Brad

回答

1

控制器

public function get_users(){ 

    //load the database 
    $this->load->database(); 

    //load the model 
    $this->load->model('user_model'); 

    // You need to pass the model data into view 
    $data['employees'] = $this->user_model->select(); 

    //the data in view 
    return $this->load->view('user_view', $data); 

} 

查看

<table> 
    <tr> 
     <td><strong>Ticket Out</strong></td> 
     <td><strong>Ticket In</strong></td> 
     <td><strong>Emp ID</strong></td> 
     <td><strong>Emp Name</strong></td> 
     <td><strong>Amount</strong></td> 
     <td><strong>Time</strong></td></tr> 

     <?php foreach($employees as $employee){ ?> 
      <tr> 
       <td><?=$employee->barcodeout;?></td> 
       <td><?=$employee->barcodein;?></td> 
       <td><?=$employee->emp_id;?></td> 
       <td><?=$employee->emp_name;?></td> 
       <td><?=$employee->amount;?></td> 
       <td><?=$employee->time;?></td> 
      </tr>  
     <?php }?> 
</table> 

您不能從您的視圖頁面調用$query->result()。它在那裏沒有定義。不要從你的視角調用模型方法。在你的模型中,你應該有與數據庫相關的方法,在控制器中所有的業務邏輯和你的視圖應該只包含你從你的控制器傳遞的。希望這可以幫助。 :)

+0

謝謝達斯,一切工作正常,但現在不是我的表單不工作時,我提交一點也不給我反應,即使在文本字段中輸入內容時,也應該清除文本字段,但它不響應,如果我在其中再添加一個測試表單,它仍然不起作用,請幫助與此同時,這將是巨大的欣賞。謝謝 – aasif

+0

我沒有得到你想說的。請更新您的問題,並附上適當的錯誤訊息 –

+0

Sayantan!,欣賞您的迴應人,問題是,我的表單無法使用。後來我才知道,我沒有加載表單助手,現在工作的很好。再次感謝。 – aasif