|
本帖最后由 Minux2007 于 2013-3-5 20:16 编辑
使用的话只要加载这个模型,使用get_list()就可以了,方法中会加载分页类。
返回当前页的数据及分页链接和总行数,至少做原型的时候会比较快
参数说明我在下面备注了
PHP复制代码
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Mds_data extends CI_Model {
//如果传递进来的参数有缺省,就是用默认值
function extend ($settings, $options) {
foreach ($options as $key => $value) {
if (key_exists($key, $settings)) {
$settings[$key] = $value;
}
}
return $settings;
}
function get_list ($options) {
$settings = $this->extend(array(
'select' => '*',//查询的字段
'tablename' => '',//查询的表名,join其他表的话比较麻烦,我使用的是视图
'per_page' => 10, //每页显示的记录数
'parameters' => array(), //,可以查询的字段,eg:array('username','title','desc'),然后url中的这些参数将会被模型使用,用来充当过滤条件
'where' => array(),//过滤的条件,直接传入CI的where()中
'order_by' => 'id desc'//排序
), $options);
$this->db->order_by($settings['order_by']);
$this->db->select($settings['select']);
$this->db->where($settings['where']);
$this->_format_url_parameters ($settings['parameters']);
if (is_numeric($this->input->get('per_page'))) {
$per_page = $this->input->get('per_page');
//限制最多每页30条数据
if ($per_page > 30) {
$per_page = 30;
}
} else {
$per_page = $settings['per_page'];
}
if (is_numeric($this->input->get('page'))) {
$page = $this->input->get('page');
} else {
$page = 1;
}
//取得actrive_record中的where,like过滤
$ar_where = $this->db->ar_where;
$ar_like = $this->db->ar_like;
$this->db->limit($per_page, ($page - 1) * $per_page);
$query_table = $this->db->get($settings['tablename']);
$this->db->ar_where = $ar_where;
$this->db->ar_like = $ar_like;
$this->db->from($settings['tablename']);
$total_rows = $this->db->count_all_results();
//如果需要分页导航的话
$this->load->library('pagination');
$url_str = $_GET;
unset($url_str['page']);//如果没有这行代码,在翻页后url中将会多出一个page参数
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
$config['base_url'] = current_url () . "?" . http_build_query($url_str);
$this->pagination->initialize($config);
return array(
'total_rows' => $total_rows,
'data' => $query_table->result(),
'page_nums' => ceil($total_rows / $per_page),
'current_page' => $page,
'page_links' => $this->pagination->create_links()
);
}
//格式化url中使用的过滤条件, $option['parameters']中的参数设置了字段名称
private function _format_url_parameters ($parameters) {
if (count($_GET) > 0) {
foreach (array_intersect($parameters, array_keys($_GET)) as $item) {
if ($_GET[$item] == '') {
continue;
} elseif (strstr($_GET[$item], ',')) {
$this->db->where_in($item, preg_split('/,/', $_GET[$item]));
} elseif (strstr($_GET[$item], '*')) {
//如果是模糊查询
if (substr($_GET[$item], 0, 1) == '*' && substr($_GET[$item], -1, 1) != '*') {
$this->db->like($item, preg_replace('/\*/', '', $_GET[$item]), 'before');
} elseif (substr($_GET[$item], 0, 1) != '*' && substr($_GET[$item], -1, 1) == '*') {
$this->db->like($item, preg_replace('/\*/', '', $_GET[$item]), 'after');
} else {
$this->db->like($item, preg_replace('/\*/', '', $_GET[$item]), 'both');
}
} else {
$this->db->where($item, $_GET[$item]);
}
}
}
}
}
复制代码
|
|