簡體   English   中英

Codeigniter分頁不呈現分頁鏈接

[英]Codeigniter pagination not rendering pagination links

您好我有以下代碼,

$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個結果,但我沒有得到任何鏈接,我做錯了什么?

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

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

嘗試在查詢中傳遞$ config [per_page]

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

然后在查詢中(注意我們將per_page變量傳遞給limit())

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();
}

希望這可以幫助

Altrim的回答非常好。 但為了保持符合CodeIgniter,我建議使用它:

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 $config['per_page'] = 20;

由於$this->pagination->initialize($config); $this->$key = $value轉換$key (per_page) => $value (20) $this->$key = $value initialize函數中的$this->$key = $value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM