2016-12-06 59 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