本文實例講述了CodeIgniter分頁類pagination使用方法。分享給大家供大家參考,具體如下:
controller控制器(application/controller/page.php文件):
public function index() { $this->load->model ( 'home_model' , '' , TRUE); $config= array(); $config['per_page'] = $this->per_page; //每頁顯示的數據數 $current_page = intval($this->input->get_post('per_page',true)); //獲取當前分頁頁碼數 //page還原 if(0 == $current_page) { $current_page = 1; } $offset = ($current_page - 1 ) * $config['per_page']; //設置偏移量 限定 數據查詢 起始位置(從 $offset 條開始) $result = $this->home_model->index($offset,$config['per_page'],$order='id desc'); $config['base_url'] = $this->config->item('base_url').'admin/home/index?'; $config['first_link'] = $this->first_link;//首頁 $config['prev_link'] = $this->prev_link;//上一頁 $config['next_link'] = $this->next_link;//下一頁 $config['last_link'] = $this->last_link;//尾頁 $config['total_rows'] = $result['total'];//總條數 $config['num_links'] = 3;//頁碼連接數 $config['use_page_numbers'] = TRUE; $config['page_query_string'] = TRUE; $this->load->library('pagination');//加載ci pagination類 $this->pagination->initialize($config); $result = array( 'list' => $result['list'], 'total' => $result['total'], 'current_page' => $current_page, 'per_page' => $config['per_page'], 'page' => $this->pagination->create_links(), ); $this->load->view ( 'admin/home' , $result ); }
model模型(application/model/home_model.php文件):
public function index($offset,$num,$order='id desc') { $query = $this->db->query( "SELECT Name_cn,Mall_type,create_time FROM smzdm_mall WHERE Is_deleted = 0 order by {$order} limit {$offset},{$num}"); return array( 'total' => $this->db->count_all('smzdm_mall',array('Is_deleted'=>'0')), 'list' => $query->result(), ); }
更多關於CodeIgniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家基於CodeIgniter框架的PHP程序設計有所幫助。