这是我的代码
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>
我已经尝试了很多这方面的事情,我得到的是未定义的变量&#39;最后,建议我使用所有三个MVC代码来获取数据库记录,非常感谢,谢谢。
答案 0 :(得分: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()
。它在那里未定义。永远不要从您的视图中调用模型方法。在您的模型中,您应该拥有与数据库相关的方法,在控制器中所有业务逻辑和您的视图应仅包含您从控制器传递的内容。希望这可以帮助。 :)