CI 分页 传参问题
如果直接查询整张数据表,不用传入$pro_name参数,我可以得到分页的功能效果;但我传入$pro_name后,只会输出查询结果,而且结果没有被分页,点击下一页,查询条件会被清空,无法输出任何结果。
麻烦各位大神看看,该怎么修改
控制类:
function accommodation($offset=''){
$this->load->model('office/accommodation_model','office');
//加载分页类
$this->load->library('pagination');
$this->load->helper('url');
$this->load->database();
$pro_name = isset($_POST['pro_name'])?$_POST['pro_name']:''; //赋初值
$data['pro_name'] = $pro_name;
$limit = 1;
$total = $this->office->select($pro_name); //获取查询总数
$data['blog'] = $this->office->list_users($limit,$offset,$pro_name); //获取查询结果
$data['total'] = $total;
$config['base_url'] = base_url().'index.php/office/accommodation/';
$config['total_rows'] = $total; //一共有多少条数据
$config['per_page'] = $limit; //每页显示条数
$config['first_link'] = '首页';
$config['last_link'] = '尾页';
$config['next_link'] = '下一页';
$config['prev_link'] = '上一页';
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['title'] = 'Pagination';
$this->load->view('official/accommodation',$data);
}
模型:
public function select($pro_name){
$query = $this->db->query("select count(*) as total from student_info where project_name ='$pro_name';");
$result = $query->result();
return $result->total;
}
public function list_users($limit,$offset,$pro_name){
$this->db->limit($limit,$offset);
$sql = "select * from student_info where project_name ='$pro_name';";
$res = $this->db->query($sql);
return $res->result();
}
视图:
<form action="<?php echo site_url('office/accommodation');?>" method="post">
项目:
<input type="text" name="pro_name" value="<?=$pro_name?>"/>
<br/>
<input type="submit" name="query" value="搜索" style="background-color:#FFf"/>
</form>
<table>
<tr>
<th>姓名</th>
<th>联系方式</th>
</tr>
<?php
foreach($blog as $item){
?>
<tr>
<td><?=$item->stu_name?></td>
<td><?=$item->mobile_phone?></td>
</tr>
<?php
}
echo $links;
?>
</table>
分页的时候如果有查询条件,需要自己把条件拼接到URL中,CI 不会帮你把查询条件拼接进去,只会处理页数而已。 使用GET传参
页:
[1]