CI 分頁類的使用

分頁自己很簡單,無非就是一個 [limit $offset, $length] 的過程。 $length 是每頁顯示的數據量,這個是固定的。要肯定的就只有 $offset了。 在CI中的分頁類一樣要依據這個來作,他在選擇 offset 依據時有兩種可選方式: (1)在url中填入offset,這個參數能夠直接做爲從數據庫中取數據的 $offset 值 (2)在url中填入當前的頁碼,$offset = ($cur_page-1) * $length 實質上,二者是一致的。只是計算的方式不一樣: (1)limit $offset, $per_page2)limit ($cur_page-1) * $per_page, $per_page 理解了這個,CI的分頁類就很容易使用了。採用哪一種方式是經過該類中下面的這個屬性來作的: var $use_page_numbers    = FALSE; // Use page number for segment instead of offset
默認是使用方式(2)。 要使用該類,最重要的仍是要清楚咱們須要怎樣的一個URL,根據該URL要獲取哪些信息。 這裏以 /controller/method/ 爲例子,要在method中分頁,應該是要構造這樣的URL: /controller/method/123/4        // 123是數據的id, 4 能夠是 (1)$offset 或者 (2)$cur_page
那麼。就這樣配置分頁類的數據(這裏採用了默認的方式): public function method($cat_id = 0, $offset=0) { $config['base_url'] = base_url('controller/method/' . $cat_id); $config['total_rows'] = $this->some_model->get_tb_count($cat_id); $config['per_page'] = 2; $config['uri_segment'] = 4;    // 分頁信息在 segment 段中的位置。 這裏是 /controller/method/$cat_id/$_page_sg
        $this->pagination->initialize($config);$all_data = $this->some_model->get_data_limited($cat_id, $config['per_page'], $offset); } 上面最後這一句對應的函數應該是這樣的: public function get_data_limited($cat_id=-1, $offset, $length) { return $this->db ->select('pc.photopicid, pc.title, pc.smallpic, pc.bigpic') ->from('t_photo p') ->join('t_photopic pc', 'pc.photoid=p.photoid', 'inner') ->where(array('p.classid'=>intval($cat_id), 'pc.isclose'=>0)) ->limit($offset, $length)        //這裏取分頁數據
                    ->get() ->result_array(); }要對其進行定製也很容易了。。
相關文章
相關標籤/搜索