2016-09-27 113 views
0

我的控制器: -笨分頁器無法正常工作

public function index($id,$page=0) { 

    $config = array(); 
    $config["base_url"] = base_url() . "index.php/categorypage/index/".$id."/"; 
    $total_row = $this->Categorymodel->record_count($id); 
    $config["total_rows"] = $total_row; 
    $config["per_page"] = 20; 
    $config['use_page_numbers'] = TRUE; 
    $config['num_links'] = $total_row; 
    $config['cur_tag_open'] = '&nbsp;<a class="current">'; 
    $config['cur_tag_close'] = '</a>'; 
    $config['next_link'] = 'Next'; 
    $config['prev_link'] = 'Previous'; 
    $this->pagination->initialize($config); 
    $page = $this->uri->segment(3) ; 

    $data["results"] = $this->Categorymodel->fetch_data($config["per_page"], $page,$id); 
    $str_links = $this->pagination->create_links(); 
    $data["links"] = explode('&nbsp;',$str_links); 

    $this->load->view('frontend/template/other_head'); 
    $this->load->view('frontend/template/category_nav'); 
    $this->load->view('frontend/category_content',$data); 
    $this->load->view('frontend/template/footer'); 
} 

我的模型

class Categorymodel extends CI_Model { 

    // Count all record of table "contact_info" in database. 
    public function record_count($catid) { 

     //$this->db->where('cat_id',$catid); 
     $query = $this->db->get("tbl_product"); 
     return $query->num_rows(); 

    } 

    // Fetch data according to per_page limit. 
    public function fetch_data($limit,$page, $pageid) { 

     $this->db->limit(12,$limit); 
     $this->db->where('cat_id', $pageid); 
     $query = $this->db->get("tbl_product"); 
     //echo $this->db->last_query();exit; 
     if ($query->num_rows() > 0) { 
      foreach ($query->result() as $row) { 
       $data[] = $row; 
      } 

      return $data; 
     } 
     return false; 
    } 
} 

我不能得到的 「補償」 值,分頁鏈接不獲取其他價值。請幫我解決這個問題。

+0

首先你不需要包含索引函數'base_url()。 「index.php/categorypage/index /".$ id。」/「;'改爲'base_url()。 「index.php/categorypage /".$ id。」/「;'第二,你可能需要設置你的基本網址並設置一些路線http://www.codeigniter.com/user_guide/general/routing.html#examples – user4419336

+0

你也可以保存一些行,不要把$ row放到新數組中:'return $ query-> result_array();' – cssBlaster21895

回答

0

好像你有錯誤的位置:

$this->db->limit(12,$limit); 

應限制($極限,$頁)

0

你可以告訴你的CI使用頁碼哪一段。

$config['uri_segment'] = 4; 

,並計算抵消了您的查詢用類似的方式:

$offset = (
     $this->uri->segment($config['uri_segment']) - 1 
    ) * $config['per_page']; 
    $offset < 0 ? $offset = 0 : ''; 

而像之前有人說,在$this->db->limit($limit, $offset)第二個參數被抵消。

+0

Okey謝謝你所有 –