2010-11-24 163 views
0

到目前爲止,找到很多幫助來獲得分頁工作的get(table)命令。codeigniter分頁查詢

我需要的是從幾個鏈接表中選擇幾條基於sql where語句的條目。

我猜query命令是使用一個,但是在這種情況下,我怎麼做分頁,因爲該命令不採取額外的參數,$config['per_page']

感謝您的幫助

回答

2

沒有任何更多的信息繼續我認爲你正在尋找的是類似於以下內容的東西。

public function pagination_example($account_id) 
{ 
    $params = $this->uri->ruri_to_assoc(3, array('page')); 

    $where = array(
     'account_id' => $account_id, 
     'active'  => 1 
    ); 

    $limit = array(
     'limit'  => 10, 
     'offset' => (!empty($params['page'])) ? $params['page'] : 0 
    ); 

    $this->load->model('pagination_model'); 
    $data['my_data'] = $this->pagination_model->get_my_data($where, $limit); 

    foreach($this->uri->segment_array() as $key => $segment) 
    { 
     if($segment == 'page') 
     { 
      $segment_id = $key + 1; 
     } 
    } 

    if(isset($segment_id)) 
    { 
     $config['uri_segment'] = $segment_id; 
    } 
    else 
    { 
     $config['uri_segment'] = 0; 
    } 

    $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/controller_name/method_name/whatever_your_other_parameters_are/page/';    
    $config['total_rows'] = $this->pagination_model->get_num_total_rows();// Make a method that will figure out the total number 
    $config['per_page']  = '10'; 

    $this->load->library('pagination'); 
    $this->pagination->initialize($config); 
    $data['pagination'] = $this->pagination->create_links(); 

    $this->load->view('pagination_example_view', $data); 

} 

    // pagination_model 
public function get_my_data($where = array(), $limit = array()) 
{ 
    $this->db 
     ->select('whatever') 
     ->from('wherever') 
     ->where($where) 
     ->limit($limit['limit'], $limit['offset']); 
    $query = $this->db->get(); 

    if($query->num_rows() > 0) 
    { 
     $data = $query->result_array(); 
     return $data; 
    } 
    return FALSE; 
} 

這至少應該讓你在正確的軌道上 如果這是你問不是我倒是很樂意幫助更多,如果你能成爲一個更具體一點。你的一些代碼如何?

+0

我最大的問題是如何設置$ config ['total_rows'];我感覺我必須兩次查詢數據庫:一次沒有限制(得到$ config ['total_rows']),第二次限制顯示。這聽起來效率低下。是這樣嗎? – salmane 2010-11-24 10:21:54

0

我能想到的唯一其他選項是在你的select語句中編寫一個計數或不限制查詢並使用array_slice選擇返回數組的一部分。