2013-05-04 66 views
0

在CI的分頁,數據指標應遵循的偏移。例如:如果限制爲10,則第二個指標應該有10抵消,這意味着該指數將開始從11至20CodeIgniter的分頁偏移不工作

我跟着一些教程,但我仍然無法得到這個偏移事情正常工作。 我每次點擊不同的分頁索引時,我的索引總是被重設爲1

這是我的分頁代碼,音符,我試圖呼應$offset,值爲(在第二索引= 10,在第三索引= 20,用$limit = 10)所以我不知道爲什麼它不工作

public function index($offset = 0) { 
     //check authorization 
     if(!isset($_SESSION['username'])) 
      redirect('backend_umat/login'); 

      // the $offset value is true, but the index is still reseted to 1 
     echo $offset; 
     $limit = 10; 
     $result = $this->umat_m->get_umat($limit, $offset); 

     //pagination 
     $config['base_url'] = site_url('/backend_umat/index'); 
     $config['total_rows'] = $result['num_rows']; 
     $config['per_page'] = $limit; 
     $config['uri_segment'] = 3; 
     $config['full_tag_open'] = '<div id="pagination">'; 
     $config['full_tag_close'] = '</div>'; 

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

     $data['pagination'] = $this->pagination->create_links(); 

這是我的模型:

public function get_umat($limit, $offset) { 
     $this->db->select('*')->from('msumat')->limit($limit, $offset)-> 
     join('mskelas', 'msumat.kelas_id = mskelas.kelas_id'); 

$q = $this->db->get(); 
      $result['rows'] = $q->result(); 

      $result['num_rows'] = $this->db->select('*')->from('msumat')-> 
            join('mskelas', 'msumat.kelas_id = mskelas.kelas_id') 
            ->get()->num_rows(); 

return $result; 

感謝˚F或你的幫助:D

+0

我不能在代碼中查看錯誤,如果偏移量正確,那麼您的查詢又如何? 'echo $ this-> db-> last_query()'在你的模型中的第一個查詢之後。什麼是錯誤的,從數據庫或分頁html數據? – Aurel 2013-05-05 13:51:51

回答

0

首先感謝tomexsans和lighta他們的幫助。對不起,我做了一個「愚蠢」的錯誤 - 花了大約2-3小時才意識到 - 索引不是跟隨$ offset,因爲我忘了使用這個變量。我使用一個整數,每當頁面加載時我重置爲1,因此索引將永遠重置爲1:P

1
public function index() { 
     //check authorization 
     if(!isset($_SESSION['username'])) 
      redirect('backend_umat/login'); 

     // the $offset value is true, but the index is still reseted to 1 


    //pagination 
    $config['base_url'] = base_url().'backend_umat/index'; 
    // basically you need a separate query to return only numrows 
    $config['total_rows'] = ?; 
    $config['per_page'] = 10; 
    $config['uri_segment'] = 3; 
    $config['full_tag_open'] = '<div id="pagination">'; 
    $config['full_tag_close'] = '</div>'; 

    $this->pagination->initialize($config); 
    $offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 

    $result = $this->umat_m->get_umat($config['per_page'], $offset); 

    $data['pagination'] = $this->pagination->create_links(); 

在這裏試試這個,我改變了一些代碼,希望這個作品。基本上我先初始化分頁配置,然後再調用模型中的任何東西。只要做一個單獨的查詢來獲得numrows。

+0

感謝您的幫助,我會很快嘗試您的代碼,並讓您知道結果 – 2013-05-05 13:34:44

+0

對不起,您的代碼不起作用,結果與我的代碼相同,$ offset的值爲true,但索引不起作用 – 2013-05-06 00:54:38

+0

@BlazeTama抱歉,但我不明白你的索引是什麼意思? – tomexsans 2013-05-06 01:01:46

1

控制器 site.com/<controller>/index/<page>

public function index($offset = 0) { 
    //check authorization 
    if(!isset($_SESSION['username'])) 
     redirect('backend_umat/login'); 

    $limit = 10; 
    $offset = (int) $offset; 
    $result = $this->umat_m->get_umat($limit, $offset); 

    //pagination 
    $config['base_url']  = site_url('/backend_umat/index'); 
    $config['total_rows']  = $result['num_rows']; 
    $config['per_page']  = $limit; 
    $config['uri_segment'] = 3; 
    $config['full_tag_open'] = '<div id="pagination">'; 
    $config['full_tag_close'] = '</div>'; 

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

    $data['pagination'] = $this->pagination->create_links(); 

型號

public function get_umat($limit, $offset) { 
    $result['rows'] = $this->db 
     ->select('SQL_CALC_FOUND_ROWS, msumat.*, mskelas.*', FALSE) 
     ->limit($limit, $offset == 1 ? 0 : $offset) 
     ->join('mskelas', 'msumat.kelas_id = mskelas.kelas_id') 
     ->get('msumat') 
     ->result(); 

    $req = $this->db->query('SELECT FOUND_ROWS()')->row_array(); 
    $result['num_rows'] = $req['FOUND_ROWS()']; 

    return $result; 
} 

爲了不必重寫第二個SQL REQ,您可以使用SQL_CALC_FOUND_ROWS檢索總如果請求不包含LIMIT聲明。但是這可能比兩個請求慢。

我使用FALSE作爲select()中的第二個參數,以便CI不嘗試使用反引號保護字段或表名。

->select('*'):是無用的,如果你希望所有領域,CI將在默認情況下做到這一點,如果選擇方法不叫。

->get()->num_rows():改用->count_all_results([table])

+0

感謝您的幫助。我會很快嘗試你的代碼,但是你有更多「初學者」的方式來完成這個嗎?我在nettuts上看到的教程提供了一個非常簡單的代碼(就像我的問題一樣),但是我認爲我犯了一個「小」錯誤,所以偏移量不起作用 – 2013-05-05 13:36:20

+0

好的但要小心,nettuts上的所有tuto並不總是更新。 – Aurel 2013-05-05 13:55:28