首先,配置php
$this->load->library('pagination'); $config = array(); // $this->config->load('pagination'); $config["base_url"] = site_url('lift/index'); $config["total_rows"] = $this->Lift_model->record_count(); $config["per_page"] = 15; $config["uri_segment"] = 3; $config['use_page_numbers'] = TRUE; $this->pagination->initialize($config); $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 1; $data["lm_lift"] = $this->Lift_model->get_lifts($config["per_page"], $page); $data["links"] = $this->pagination->create_links(); $this->load->view('lift/index',$data);
前端輸出css
<?php echo $links; ?>
樣式美化,這裏以bootstrap pagination爲例子,在config目錄下添加pagination.php的配置前端
<?php if(!defined('BASEPATH')) exit('Direct Access Not Allowed'); /* This Application Must Be Used With BootStrap 3 * */ $config['full_tag_open'] = "<ul class='pagination'>"; $config['full_tag_close'] ="</ul>"; $config['num_tag_open'] = '<li>'; $config['num_tag_close'] = '</li>'; $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>"; $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>"; $config['next_tag_open'] = "<li>"; $config['next_tagl_close'] = "</li>"; $config['prev_tag_open'] = "<li>"; $config['prev_tagl_close'] = "</li>"; $config['first_tag_open'] = "<li>"; $config['first_tagl_close'] = "</li>"; $config['last_tag_open'] = "<li>"; $config['last_tagl_close'] = "</li>"; // end of file Pagination.php // Location config/pagination.php // By @ mzhong
分頁保持查詢條件
http不能保持狀態,能夠經過session,uri來保持。若是要用uri來保持狀態的話,就須要把查詢表單修改成get提交方式。shell
<?php echo form_open('c/search',array('method'=>'get')); ?>
須要用的配置項:
$config['suffix'] = '';
給路徑添加一個自定義後綴,後綴位於偏移段的後面。bootstrap
$config["base_url"] = site_url('c/search'); if (count($get) > 0) { $config['suffix'] = '?' . http_build_query($get, '', "&"); } $config['first_url'] = site_url('c/search/1?') . http_build_query($get, '', "&query=true"); //修復第一頁bug.是Pagination.php自做聰明地將第一頁認爲老是沒必要加分頁號.
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f #tells mod_rewrite ignore the url if it's an actual file, and RewriteCond %{REQUEST_FILENAME} !-d #tells it to ignore it if it's a directory. RewriteCond $1 !^(index\.php|contact\.php|images|css|js|video|robots\.txt) RewriteRule ^(.*)$ index.php/$1 [L]