控制器代码:

CodeIgniter框架之分页

public function page(){
        $this->load->library('pagination');//加载分页库
        $this->load->helper('url');

        $this->load->model('user_model','user');//给模型取别名
        $count = $this->user->allNums('users');
        $config['base_url'] = site_url('user/page');
        $config['total_rows'] = $count;
        $config['per_page'] = '2';
        $config['first_link'] = '首页';
        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';
        $config['last_link'] = '末页';
        $config['uri_segment'] = 3;//必须与$this->uri->segment(3)保持一致

        $this->pagination->initialize($config);

        $offset = intval($this->uri->segment(3));

        $data['users'] = $this->user->getUserByPage($offset,$config['per_page'],'users');

        $data['link'] = $this->pagination->create_links();
        $this->load->view('user/page',$data);
    }

模型代码

CodeIgniter框架之分页

public function getUserByPage($offset,$per_page_nums,$table){
            $result = $this->db->limit($per_page_nums,$offset)->get($table);
            return $result->result();
        }

        public function allNums($table){
            $result = $this->db->get($table);
            return $result->num_rows();
        }

视图代码:

CodeIgniter框架之分页

<html>
<meta charset="utf-8">
<p>分页demo</p>
    <?=$link;?>
    <?="<br>"?>
    <?php foreach ($users as $key => $value): ?>
        <?=$value->id;?>
        <?="<br>"?>
    <?php endforeach ?>
</html>

结果

CodeIgniter框架之分页

CodeIgniter框架之分页

相关文章: