天天看點

CodeIgniter架構之分頁

控制器代碼:

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架構之分頁