CI 分页代码

controller 里面

private $per_page  = '45';
private $prev_link  = '上一页';
private $next_link  = '下一页';

function show()
{
        $config       = array();
        $config['per_page']    = $this->per_page;         //每页显示的数据数
        $current_page          = intval($this->input->get_post('per_page',true));  //获取当前分页页码数
   
        if(0 == $current_page)
        {
           $current_page = 1;
        }
  
        $offset   = ($current_page - 1 ) * $config['per_page'];          //设置偏移量 限定 数据查询 起始位置(从 $offset 条开始)       
        $buy_info  = $this->buy_model->get_buyinfolist($offset,$config['per_page'],$order='id desc');
        $config['base_url']           = 'http://'.$this->config->item('base_url').'buy/show?';
        $config['prev_link']          = $this->prev_link;
        $config['next_link']          = $this->next_link;
        $config['total_rows']         = $buy_info['total'];         //获取查询数据的总记录数
        $config['use_page_numbers']   = TRUE;            //默认分页URL中是显示每页记录数,启用use_page_numbers后显示的是当前页码
        $config['page_query_string']  = TRUE;            //把 $config['enable_query_strings'] 设置为 TRUE,链接将自动地被用查询字符串(url中的参数)重写。        
        $this->pagination->initialize($config);
  
        $data = array(
             'buy_info'  => $buy_info['res'],
             'total'   => $buy_info['total'],
             'current_page' => $current_page,
             'per_page'  => $config['per_page'],
             'page'   => $this->pagination->create_links(),
            );
          $this->load->view("show_buy_view", $data);
}

model 里面

public function get_buyinfolist($offset,$num,$order='id desc')
{
          $table   = "auto_buyinfo";
  
          $str_sql  = "select * from {$table} where 1=1  order by {$order} limit {$offset},{$num}";
  
          //if($condition)
          //{
           //$str_sql = "select * from {$table} where 1=1 and username like '%" . $condition . "%' order by {$order} limit {$offset},{$num}";
          //}
          return array(
                    'total' => $this->db->count_all('auto_buyinfo'),
                    'res' => $this->db->query($str_sql)->result_array(),
                    );
 }

view 里面

在显示的地方加入<?php echo $page;?>

转载于:https://my.oschina.net/shunshun/blog/66871

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值