所以我设置了一个MVC项目来搜索数据库并显示结果。
点击搜索按钮,页面就会重新加载。我出错的任何想法?
控制器
public function search()
{
$txt = $this->input->post('search', true);
$this->load->model('post');
$data['searchbox'] = $this->post->search($txt); //search is function on model
$this->load->view('/search', $data);
}
模型
public function search($txt)
{
$query = $this->db->query("SELECT * From st_posts");
return $query->result();
}
表单视图
<form action="<?php echo site_url('/search');?>" method = "post">
<div class="form-group">
<div class="col-md-8">
<input type="text" name = "search" id="search" class="form-control" />
</div>
<div class="col-md-4">
<input type="submit" value = "Search" class="btn btn-primary center-block" />
</div>
</div>
</form>
结果视图
<table>
<tr>
<th>ID</th>
<th>title</th>
<tr>
<?php foreach($searchbox as $row)?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->name;?></td>
</tr>
<?php endforeach;?>
</table>
答案 0 :(得分:0)
你应该总是使用模型只是为了查询数据库,比如删除,搜索,添加,更新数据等。我总是像这样申请尝试以这种形式更新你的代码:
<强>控制器强>: 这就是说我们在控制器上有一个这样的功能:
public function search()
{
$txt = $this->input->post('search', true);
$this->load->model('Your model that you want to search from');
$data['searchbox'] = $this->yourModel->search($txt); //search is function on model
$this->load->view('pages/membership_area/search', $data);
}
<强>模型强>: 这就是说我们在Model上也有这样的函数:
public function search($txt)
{
$query = $this->db->query("SELECT * From TableName");
return $query->result();
}
查看:
<?php foreach($searchbox as $row)?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->name;?></td>
</tr>
希望它适合你:)