2012-05-04 122 views
4

你好,我有以下代碼,笨分頁不渲染分頁鏈接

$this->load->library('pagination'); 
$this->data['products'] = $this->products_model->get_products_and_category($this->uri->segment(4)); 

$config['base_url'] = base_url()."admin/products/manage/"; 
$config['total_rows'] = $this->db->get('products')->num_rows(); 
$config['per_page'] = 20; 
$config['full_tag_open'] = '<div class="btn-group">'; 
$config['full_tag_close'] = '</div>'; 
$config['anchor_class'] = 'class="btn" '; 
$config['cur_tag_open'] = '<div class="btn">'; 
$config['cur_tag_close'] = '</div>'; 
$config['uri_segment'] = 4; 

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

$this->template->build('admin/products/index', $this->data); 

這是由get_products_and_category($this->uri->segment(4))運行看起來像這樣的查詢,

public function get_products_and_category($offset=0) { 
    $this->db->select('products.product_id, products.product_title, products.product_created, products.parent_category, categories.category_id, categories.category_title') 
    ->from('products') 
    ->join('categories' , 'products.parent_category = categories.category_id', 'left') 
    ->order_by('products.product_title', 'ASC') 
    ->limit(25, $offset); 

    $query = $this->db->get(); 
    return $query->result_array(); 
} 

有25個結果在我的表,我想每頁顯示20個,所以通過我的數學分頁課程應該創建2個鏈接(第1頁和第2頁)第一頁應該有20個結果,第二個應該有4個結果,但是我沒有收到所有的鏈接,我做錯了什麼?

+1

我假設你在模板中回顯數據['分頁'],對吧? – IsisCode

+0

Absolutley - 那對我來說真的很愚蠢 – Udders

+2

@ sico87,有時候人們犯了愚蠢的錯誤,總是值得提問,尤其是因爲我們沒有看到你的代碼。 – Jakub

回答

4

LIMIT子句可用於約束SELECT語句返回的行數。

現在,您有25個結果,並且限制查詢返回25個結果,因此您的分頁可能無法正常工作。

嘗試傳遞$配置[per_page]在查詢

$this->data['products'] = $this->products_model->get_products_and_category($config['per_page'],$this->uri->segment(4)); 

然後在查詢(請注意我們通過per_page變量到極限())

public function get_products_and_category($num, $offset=0) { 
$this->db->select('products.product_id, products.product_title, products.product_created, products.parent_category, categories.category_id, categories.category_title') 
->from('products') 
->join('categories' , 'products.parent_category = categories.category_id', 'left') 
->order_by('products.product_title', 'ASC') 
->limit($num, $offset); // Here we pass the per_page var 

$query = $this->db->get(); 
return $query->result_array(); 
} 

希望這有助於

+1

我不相信會影響分頁創作。分頁庫正在傳遞總共有25行的參數,並且20個項目將被顯示在一頁中。因此,無論查詢結果如何,它都應該生成2個錨。該庫與查詢結果沒有任何關係。 – Broncha

0

Altrim的回答非常好。但留符合笨,我建議使用這個代替:

public function get_products_and_category($offset=0) { 
    $this->db->select('products.product_id, products.product_title, products.product_created, products.parent_category, categories.category_id, categories.category_title') 
    ->from('products') 
    ->join('categories' , 'products.parent_category = categories.category_id', 'left') 
    ->order_by('products.product_title', 'ASC') 
    ->limit($this->per_page, $offset); // Here we pass the per_page var 

    $query = $this->db->get(); 
    return $query->result_array(); 
} 

你已經在$config['per_page'] = 20;

定義per_page由於$this->pagination->initialize($config);initialize函數變換$key (per_page) => $value (20)$this->$key = $value