CI 分页
controller 里面
1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 2 3 class xxxx extends CI_Controller { 4 5 private $per_page = '45'; 6 private $prev_link = '上一页'; 7 private $next_link = '下一页'; 8 9 function show(){ 10 $config = array(); 11 $config['per_page'] = $this->per_page; //每页显示的数据数 12 $current_page = intval($this->input->get_post('per_page',true)); //获取当前分页页码数 13 14 if(0 == $current_page){ 15 $current_page = 1; 16 } 17 18 $offset = ($current_page - 1 ) * $config['per_page']; //设置偏移量 限定 数据查询 起始位置(从 $offset 条开始) 19 $buy_info = $this->buy_model->get_buyinfolist($offset,$config['per_page'],$order='id desc'); 20 $config['base_url'] = 'http://'.$this->config->item('base_url').'buy/show?'; 21 $config['prev_link'] = $this->prev_link; 22 $config['next_link'] = $this->next_link; 23 $config['total_rows'] = $buy_info['total']; //获取查询数据的总记录数 24 $config['use_page_numbers'] = TRUE; //默认分页URL中是显示每页记录数,启用use_page_numbers后显示的是当前页码 25 $config['page_query_string'] = TRUE; //把 $config['enable_query_strings'] 设置为 TRUE,链接将自动地被用查询字符串(url中的参数)重写。 26 $this->pagination->initialize($config); 27 28 $data = array( 29 'buy_info' => $buy_info['res'], 30 'total' => $buy_info['total'], 31 'current_page' => $current_page, 32 'per_page' => $config['per_page'], 33 'page' => $this->pagination->create_links(), 34 ); 35 $this->load->view("show_buy_view", $data); 36 } 37 } 38 39 ?>
model 里面
1 <?php 2 3 class yyyy extends CI_Model { 4 5 public function get_buyinfolist($offset,$num,$order='id desc'){ 6 $table = "auto_buyinfo"; 7 $str_sql = "select * from {$table} where 1=1 order by {$order} limit {$offset},{$num}"; 8 // if($condition){ 9 // $str_sql = "select * from {$table} where 1=1 and username like '%" . $condition . "%' order by {$order} limit {$offset},{$num}"; 10 // } 11 return array( 12 'total' => $this->db->count_all('auto_buyinfo'), 13 'res' => $this->db->query($str_sql)->result_array(), 14 ); 15 } 16 } 17 18 ?>
view 里面
<?php echo $page;?>