CodeIgniter框架多条件搜索查询分页功能解决方案

最近在用ci框架写功能的时候,需要用到分页功能,本来寻常的数据结果分页是比较简单的,但是这次写的功能是多条件搜索查询分页,就有点难度了,看官方手册下面评论好多人问,

正常的分页功能例子是这样的:

1 $this->load->library('pagination');
2 
3 $config['base_url'] = 'http://example.com/index.php/test/page/';
4 $config['total_rows'] = 200;
5 $config['per_page'] = 20;
6 
7 $this->pagination->initialize($config);
8 
9 echo $this->pagination->create_links();

 

多条件搜索查询分页例子:

 1 class Monitors extends CI_Controller {
 2 public function warning(){
 3     $config= array();
 4     $config['per_page'] = 15; //每页显示的数据数
 5     $current_page = intval($this->input->get("per_page")); //获取当前分页页码数
 6     
 7  $status=$this->input->get("filter-status",TRUE);
 8  $level=$this->input->get('filter-level',TRUE);
 9  $timestamp=$this->input->get('filter-timestamp',TRUE);
10  $all = $this->monitors_m->getAllData($current_page,$config['per_page'],$status,$timestamp,$level);
11    //这里返回的有总条数和具体的数据,根据自己的情况略加修改即可
12  $data['allevent'] = $all['content'];
13  $config['total_rows']   = $all['count'];//总条数
14     $config['num_links'] = 3;//页码连接数
15  $config['use_page_numbers'] = TRUE; 
16  $config['page_query_strings'] = TRUE;//关键配置
17  $config['base_url'] = base_url().'index.php/monitors/warning?'&filter-status='.$status.'&filter-level='.$level.'&filter-timestamp='.$timestamp;//关键配置
18  $this->load->library('pagination');//加载ci pagination类
19  $this->pagination->initialize($config);
20  $data['page'] = $this->pagination->create_links();//关键代码
21  $this->load->view("monitors_v",$data);
22 }

需要注意的几个地方,第一个:

1 $config['page_query_strings'] = TRUE;//关键配置

第二个:

1 $config['base_url'] = base_url().'index.php/monitors/warning?'&filter-status='.$status.'&filter-level='.$level.'&filter-timestamp='.$timestamp;//关键配置

第三个:

1 $this->pagination->create_links();//关键代码

第四个:这个是查询的数据,返回的需要是分页的数据,这个要注意,

1 $all = $this->monitors_m->getAllData($current_page,$config['per_page'],$status,$timestamp,$level);

 

把这些配置好,基本上就可以了,感谢大家!

 

posted @ 2020-01-06 17:26  青茶360  阅读(1106)  评论(0编辑  收藏  举报