控制器裏:php
$this->load->model('review_model'); $this->load->library('pagination'); //設定分頁的根連接 $config['base_url'] = site_url('review/show/'.$gid); //進行查詢內容計算總數 $reviewinfo_temp = $this->review_model->GetGoodsRevobject($gid); $config['total_rows'] = $reviewinfo_temp->num_rows(); //每頁顯示多少條記錄 $config['per_page'] = 20; $config['uri_segment'] = 4; $config['first_link'] = '首頁'; $config['last_link'] = '尾頁'; $config['full_tag_open'] = '<p>'; $config['full_tag_close'] = '</p>'; $this->pagination->initialize($config); //傳參數給VIEW $data['page_links'] = $this->pagination->create_links(); //再次查詢,獲得須要顯示的數據 $review = $this->review_model->GetGoodsRevobject($gid,$config['per_page'],$this->uri->segment(4)); //下面載入VIEW不寫了 //......
從上面設定分頁的base_rul能夠看出來,模型函數須要三個參數,一個gid是必須的,另兩個用有limit語句裏,模型代碼:數據庫
//這裏$num和$offset默認參數爲空 function GetGoodsRev($gid=NULL,$num="",$offset="") { $this->db->limit($num,$offset); $this->db->where("gid",$gid); $this->db->order_by("rpubdate", "desc"); $query = $this->db->get($this->config->item('db_prefix').'review'); return $query; }
視圖很簡單,只有在須要添加分頁連接的地方寫一句:函數
//這個變量名稱要和控制器裏傳過來的對應。 <?php echo $page_links;?>
CodeIgniter的分頁類對內容分頁很方便,不過,從上面也能夠看到,分頁效率很低。在不分頁的狀況下,只須要進行一次數據庫查詢,如今須要兩次。我正在考慮能不能優化一下,也許接下來的博客裏會提到。優化