首先,我建议建立一个辅助函数来配置你的分页这样。您必须从配码文件中找到配置中的选项目的
if (!function_exists('getPaginationConfig')) {
function getPaginationConfig($url,$total_row,$per_page,$num_links=3){
$config = array();
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 3;// depends on how you passing your page number
$config['base_url'] = $url;
$config['total_rows'] = $total_row;
$config['per_page'] = $per_page;
$config['full_tag_open'] = '<ul>';
$config['full_tag_close'] = '</ul>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['first_link'] = '<<';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = '>>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['num_links'] = $num_links;
return $config;
}
}
Now On Controller ..你需要从数据库中获取的总行并设置perpage在分页 并与配置 来电来访是控制器功能的一个例子
function getMyList($page){
$limit = YOUR_PAGE_LIMIT;//may be some global variable or input from frontend
$starting = ($page-1)*$limit;
$data['results'] = $this->your_model->getListOfTable($where,$limit,$starting);
$total_rows = $this->your_model->getTotalRowsOfTable($where);// where is filter condition
$this -> load -> helper('your_helper');// where yours getPaginationConfig is defined
$this -> load -> library('pagination');
$this -> pagination -> initialize(getPaginationConfig(site_url('controller/function'), $total_rows,$limit));
$data['pagination'] = $this -> pagination -> create_links();
$this->load->view('yourview',$data);
你的模型可能看起来像这样
function getListOfTable($where = array(), $limit, $starting = 0) {
$this -> db -> select('*');
// <-- There is never any reason to write this line!
$this -> db -> from('your_tables');
if (!empty($where))
$this -> db -> where($where);
$this -> db -> limit($limit, $starting);
$query = $this -> db -> get();
if ($query -> num_rows() > 0) {
return $query -> result();
} else {
return FALSE;
}
}
function getTotalRowsOfTable($where = array()){
if (!empty($data))
$this -> db -> where($where);
$this -> db -> from('your_table');
return $this -> db -> count_all_results();
}
,立即查看你只需要在你喜欢的地方回显你的分页
<div id="pagination"><?php echo $pagination;?></div>
很多关于分页的堆栈已经在堆栈中 –
我有c hecked他们大多数,但仍然不明白为什么它不在这里工作 – user3480644
我只是举一个例子,并根据您的答案转换 –