带参数的分页处理方法
带参数的分页处理方法。刚用CI不久,很多方面还不熟悉,发个自己用的搜索分页,不知道有没有更加简便的一些方法。1、config.php
$config['uri_protocol'] = "PATH_INFO";
2、helpers/my_helper.php里面添加一个函数
if ( ! function_exists('get_query_info'))
{
function get_query_info($get){
$get = array_filter($get);
$query_str ="";
$i = 0;
foreach ( $get as $k=>$v ){
if( $k=='per_pag'){
continue;
}
if ( !empty($v) ){
$query_str .= "{$k}={$v}";
if ( $i>0 ){
$query_str .= "&";
}
}
$i++;
}
$data['query_str'] = $query_str;
$data['per_page'] = $get['per_page'];
unset($get['per_page']);
$data['where'] = $get;
return (object)$data;
}
}
3、controllers中
function index(){
$query_info = get_query_info($_GET);
$this->load->library('pagination');
$config['base_url'] = site_url()."/item/index?".$query_info->query_str;
$config['total_rows'] = $total_rows = $this->db->where($query_info->where)->from('item')->count_all_results();
$config['per_page'] = '10';
$this->pagination->initialize($config);
$data['page'] = $this->pagination->create_links();
$this->load->model('item_model','mItem',TRUE);
$data['results'] = $this->mItem->get_item($config['per_page'],$query_info->per_page,$query_info->where);
$this->load->view('Item/index', $data);
}
4、model
function get_item($num, $offset, $where=array(),$order='id DESC')
{
$query = $this->db->where($where)->order_by($order)->get('item', $num, $offset);
return $query->result();
} 很不错,基本上就是这样写。 if ( ! function_exists('get_query_info'))
{
function get_query_info($get){
$get = array_filter($get);
$query_str ="";
$i = 0;
foreach ( $get as $k=>$v ){
if( $k=='per_pag'){
continue;
}
if ( !empty($v) ){
$query_str .= "{$k}={$v}";
if ( $i>0 ){
$query_str .= "&";
}
}
$i++;
}
$data['query_str'] = $query_str;
$data['per_page'] = $get['per_page'];
unset($get['per_page']);
$data['where'] = $get;
return (object)$data;
}
}
这段可以简化一下
if (! function_exists ( 'get_query_info' )) {
function get_query_info($get) {
$get = array_filter ( $get );
isset($get ['per_page'])?$data ['per_page'] = $get ['per_page']:$data['per_page'] = 0;
unset ( $get ['per_page'] );
$data ['query_str'] = http_build_query($get);
$data ['where'] = $get;
return ( object ) $data;
}
} 好东西啊,谢谢LZ分享,收藏了。 这个分页不错的 支持
页:
[1]