我想使用php code ignitor显示我的数据表单db。这是我到目前为止所做的,但无法得到结果。
未定义变量查询的消息
。请查看并建议我应该做什么或以任何其他方式显示数据库表中的数据。 视图:
<tbody>
<?php foreach($query as $row): ?>
<tr>
<td><?php echo $row->subuid; ?></td>
<td><?php echo $row->uid; ?></td>
<td><?php echo $row->fname; ?></td>
<td><?php echo $row->lname; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
型号:
public function showusers(){
function viewusers()
{
$query = $this->db->select('*')->from('sub_users')->get();
return $query->result();
}
}
控制器:
function showusers()
{
$this->load->model('User_area_model');
$data4['query'] = $this->session->viewusers();
$this->load->view('manage-users', $data4);
}
答案 0 :(得分:1)
您的controller
应该是这样的:
function showusers()
{
$this->load->model('User_area_model');
/*replace this $this->session->viewusers() with User_area_model;*/
$data4['query'] = $this->User_area_model->viewusers();
if ( !empty($data4['query']))
{
$this->load->view('manage-users', $data4);
}
else
{
echo 'no record found';
}
}
型号:
public function viewusers()
{
$query = $this->db->get('sub_users');
return $query->result();
}
查看:查看视图中的empty
<tbody>
<?php if (! empty($query) ) {
foreach($query as $row): ?>
<tr>
<td><?php echo $row->subuid; ?></td>
<td><?php echo $row->uid; ?></td>
<td><?php echo $row->fname; ?></td>
<td><?php echo $row->lname; ?></td>
</tr>
<?php endforeach;
}?>
</tbody>
答案 1 :(得分:1)
试试这个,
<强> HTML 强>
<tbody>
<?php if($query){ ?>
<?php foreach($query as $row){ ?>
<tr>
<td><?= $row->subuid; ?></td>
<td><?= $row->uid; ?></td>
<td><?= $row->fname; ?></td>
<td><?= $row->lname; ?></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
<强> MODEL 强>
public function viewusers(){
$this->db->select('*')->from('sub_users');
$query = $this->db->get();
return ($query->num_rows() > 0) ? $query->result() : false;
}
<强> CONTROLLER 强>
function showusers(){
$this->load->model('User_area_model');
$data['query'] = $this->User_area_model->viewusers();
$this->load->view('manage-users', $data);
}