2017-08-16 86 views
1

我試圖在不使用框架的分頁庫的情況下對Codeigniter 3應用進行分頁。更確切地說,我想分頁約100行。爲此:Codeigniter 3:分頁只顯示前10條記錄

在家用控制器我有:

public function index() { 
    $this->load->model('Customer'); 
    $this->load->library('pagination'); 
    $config = [ 
     'base_url' => base_url("index.php"), 
     'per_page' => 10, 
     'total_rows' => $this->Customer->get_num_rows(), 
     'uri_segment' => 3, 
     'first_tag_open' => '<li>', 
     'first_tag_close' => '</li>', 
     'last_tag_open' => '<li>', 
     'last_tag_close' => '</li>', 
     'full_tag_open' => '<ul class="pagination">', 
     'full_tag_close' => '</ul>', 
     'next_tag_open' => '<li>', 
     'next_tag_close' => '</li>', 
     'prev_tag_open' => '<li>', 
     'prev_tag_close' => '</li>', 
     'num_tag_open' => '<li>', 
     'num_tag_close' => '</li>', 
     'cur_tag_open' => '<li class="active"><a>', 
     'cur_tag_close' => '</a></li>', 
    ]; 
    $this->pagination->initialize($config); 
    $customers = $this->Customer->getCustomers($config['per_page'], $this->uri->segment($config['uri_segment'])); 
    $this->load->view('home', ['records'=>$customers]); 
} 

在模型文件中我有:

class Customer extends CI_Model { 
public function __construct() { 
    $this->load->database(); 
} 

public function getCustomers($limit, $offset) { 
    $this->db->limit($limit, $offset); 
    $query = $this->db->get('customers'); 
    return $query->result(); 
} 
} 

Finaly,認爲:

<div class="pagination-container text-center"> 
    <?php echo $this->pagination->create_links(); ?> 
</div> 

分頁顯示並且格式正確;所以都是第10條enter image description here

但如果我點擊第二頁,或任何其他頁面上,我得到相同的前10個記錄如可以在下面的圖片中可以看出:

enter image description here

2網頁的網址是:

http://localhost/cicrud/index.php?page=2 

我做錯了什麼,我不明白是什麼。任何幫助?謝謝!

+0

如果你使用的控制器是cicrud,那麼你的base_url不應該是/。此外,uri_segment大概是2,而不是5. –

+0

在公共函數索引($ offset = 0)中傳遞偏移量作爲默認值 –

+0

也在模型函數getCustomers($ limit = null,$ offset = null){} –

回答

0

如果使用GET方法,一定要加​​代替$this->uri->segment($config['uri_segment']);.

+0

我的查詢字符串段,如下面所示的凸輪是'query_string_segment'=>'page','。 –

+0

它沒有工作嗎?你能更新代碼嗎? – Alexut

1

添加這些配置線太...

$config['page_query_string'] = TRUE; 
$config['query_string_segment'] = 'page'; 
$config['display_pages'] = TRUE; 
$config['use_page_numbers'] = TRUE; 
+0

,我不再得到「找不到對象!」來自瀏覽器的消息。感謝幫助。但點擊分頁項目不會有相應的記錄。我仍然只看到_只有前10個_ :( –

+0

)您需要創建一個模型來從給定的起始位置查詢表 (select * from table limit 0,10) - if page = 1 or empty (select * from table限制10,10) - 如果頁面= 2或空 示例: ** getCustomers($ config ['per_page'],$ page_num * $ config ['per_page'] - $ config ['per_page'])** –

0

因此,該指數()函數看起來像這樣:

public function index() { 
    $this->load->model('Customer'); 
    $this->load->library('pagination'); 
    $config = [ 
     'base_url' => base_url("index.php"), 
     'page_query_string' => TRUE, 
     'query_string_segment' => 'page', 
     'display_pages' => TRUE, 
     'use_page_numbers' => TRUE, 
     'per_page' => 10, 
     'total_rows' => $this->Customer->get_num_rows(), 
     'uri_segment' => 3, 
     'first_tag_open' => '<li>', 
     'first_tag_close' => '</li>', 
     'last_tag_open' => '<li>', 
     'last_tag_close' => '</li>', 
     'full_tag_open' => '<ul class="pagination">', 
     'full_tag_close' => '</ul>', 
     'next_tag_open' => '<li>', 
     'next_tag_close' => '</li>', 
     'prev_tag_open' => '<li>', 
     'prev_tag_close' => '</li>', 
     'num_tag_open' => '<li>', 
     'num_tag_close' => '</li>', 
     'cur_tag_open' => '<li class="active"><a>', 
     'cur_tag_close' => '</a></li>' 
    ]; 
    $this->pagination->initialize($config); 
    $customers = $this->Customer->getCustomers($config['per_page'], $this->input->get('page')); 
    $this->load->view('home', ['records'=>$customers]); 
} 

希望我的回答將是對別人有用。非常感謝所有幫助。

0

試圖改變

'uri_segment' => 3, 

'uri_segment' => 1, 
+0

_uri_segment_如何工作? –

0

爲了讓您更好地運作可讀我修改幾個變量名稱,並創建如下分頁:

/* 
     $perpage - is nothing but limit value, no of records to display 
     $page - is nothing but offset, thats page number 
*/ 
public function getCustomers($perpage=10, $page=0) { 
     $page = $page-1; 
     if ($page<0) { 
      $page = 0; 
     } 
     /* decides from where to start*/ 
     $from = $page*$perpage; 
     $this->db->limit($perpage, $from); 
     $query = $this->db->get('customers'); 
     return $query->result(); 
} 
+0

它沒有工作。 ! –