amberom 2020-01-06
最近在用ci框架写功能的时候,需要用到分页功能,本来寻常的数据结果分页是比较简单的,但是这次写的功能是多条件搜索查询分页,就有点难度了,看官方手册下面评论好多人问,
正常的分页功能例子是这样的:
$this->load->library(‘pagination‘); $config[‘base_url‘] = ‘http://example.com/index.php/test/page/‘; $config[‘total_rows‘] = 200; $config[‘per_page‘] = 20; $this->pagination->initialize($config); echo $this->pagination->create_links();
多条件搜索查询分页例子:
class Monitors extends CI_Controller { public function warning(){ $config= array(); $config[‘per_page‘] = 15; //每页显示的数据数 $current_page = intval($this->input->get("per_page")); //获取当前分页页码数 $status=$this->input->get("filter-status",TRUE); $level=$this->input->get(‘filter-level‘,TRUE); $timestamp=$this->input->get(‘filter-timestamp‘,TRUE); $all = $this->monitors_m->getAllData($current_page,$config[‘per_page‘],$status,$timestamp,$level); //这里返回的有总条数和具体的数据,根据自己的情况略加修改即可 $data[‘allevent‘] = $all[‘content‘]; $config[‘total_rows‘] = $all[‘count‘];//总条数 $config[‘num_links‘] = 3;//页码连接数 $config[‘use_page_numbers‘] = TRUE; $config[‘page_query_strings‘] = TRUE;//关键配置 $config[‘base_url‘] = base_url().‘index.php/monitors/warning?‘&filter-status=‘.$status.‘&filter-level=‘.$level.‘&filter-timestamp=‘.$timestamp;//关键配置 $this->load->library(‘pagination‘);//加载ci pagination类 $this->pagination->initialize($config); $data[‘page‘] = $this->pagination->create_links();//关键代码 $this->load->view("monitors_v",$data); }
需要注意的几个地方,第一个:
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);
把这些配置好,基本上就可以了,感谢大家!