2017-04-18 31 views
1

我是Codeigniter分頁的新手,所以我無法正確配置它,爲什麼我需要您的幫助人員。無法在Codeigniter中正確配置分頁

這是到目前爲止我的代碼

public function nearbay_get($idNearBay = null) 
    { 
    $config['base_url'] = 'http://mypage.si/rest/api/nearbay/nearbay'; 
    $config['total_rows'] = $this->Near_bays->count(); 
    $config["per_page"] = 5; 
    $config['first_link'] = 'First'; 
    $config['last_link'] = 'Last'; 
    $config['suffix'] = '?'.http_build_query($_GET, '', "&"); 
    $config['use_page_numbers'] = TRUE; 
    $config['enable_query_strings'] = 'page='; 

    $choice = $config["total_rows"]/$config["per_page"]; 
    $config["num_links"] = round($choice); 
    $this->pagination->initialize($config); 

    $page = ($this->uri->segment(6))? $this->uri->segment(6) : 0; 
    $results = $this->Near_bays->get(null, [null, null], [null, null], '', $config["per_page"], $page); 
    foreach($results as $data) { 
     echo $data->id_near_bay . " ------- " . $data->name . "<br>"; 
    } 
    echo $this->pagination->create_links(); 
    } 

這是結果,如果我訪問http://mypage.si/rest/api/nearbay/nearbay result 如果我在任何頁面點擊數我得到的空白頁,我的網址更改爲http://mypage.si/rest/api/nearbay/nearbayenter image description here

所以我的問題是。如何讓分頁工作?如何正確配置分頁,所以我將能夠通過頁面和?per_page值谷歌網址獲取結果..我的最終鏈接應該看起來像這樣http://mypage.si/rest/api/nearbay/nearbay?page=1&per_page=20

如果您需要任何其他信息,請讓我知道,我會提供... 謝謝!

回答

1

這裏是答案,希望這將有助於和URL應該是這樣的:http://mypage.si/rest/api/nearbay/nearbay/1

public function nearbay_get($idNearBay = null) 
{ 
    $config['base_url'] = 'http://mypage.si/rest/api/nearbay/nearbay'; 
    $config["per_page"] = 5; 
    $config["num_links"] = 3; 
    $config["uri_segment"] = 6; 
    $config['use_page_numbers'] = TRUE; 

    $config['first_link'] = 'First'; 
    $config['last_link'] = 'Last'; 

    $config['suffix'] = '?'.http_build_query($this->input->get(), '', "&"); 
    $config['first_url'] = $config['base_url'] . $config['suffix']; 

    $page = ($this->uri->segment(6))? $this->uri->segment(6) : 0; 
    $offset = ($page == 0 ? 0 : ($page - 1) * $config["per_page"]); 

    $config['total_rows'] = $this->Near_bays->count(); 

    $this->pagination->initialize($config); 

    $results = $this->Near_bays->get(null, [null, null], [null, null], '', $config["per_page"], $offset); 
    foreach($results as $data) { 
     echo $data->id_near_bay . " ------- " . $data->name . "<br>"; 
    } 

    //$current_page = $page == 0 ? 1 : $page; 
    //$start = $page == 0 ? 1 : (($page - 1) * $config["per_page"] + 1); 
    //$end = ($start + count($results) - 1); 
    //$total_page = ceil($config['total_rows']/$config['per_page']); 
    echo $this->pagination->create_links(); 
} 

對於檢查uri_segment覈對數量。目前是6如果您沒有收到,請嘗試更多的號碼。

+0

這不是我的情況,但我從你的代碼中得到了一些提示,所以我會接受答案!謝謝! –