简体   繁体   English

Codeigniter分页不呈现分页链接

[英]Codeigniter pagination not rendering pagination links

Hello I have the following code, 您好我有以下代码,

$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);

The query this is being run by get_products_and_category($this->uri->segment(4)) looks like this, 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();
}

There are 25 results in my table, and I want to show 20 per page, so by my maths the pagination class should create 2 links (page 1 and page 2) the first page should 20 results on it, and the second should have 4 results, however I am not getting any links at all, am I doing some wrong? 我的表中有25个结果,我想每页显示20个,所以通过我的数学分页类应该创建2个链接(第1页和第2页),第一页应该有20个结果,第二个应该有4个结果,但我没有得到任何链接,我做错了什么?

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT子句可用于约束SELECT语句返回的行数。

Now you have 25 results and you limit your query to return 25 results so your pagination might not work correctly. 现在您有25个结果,并且您将查询限制为返回25个结果,因此您的分页可能无法正常工作。

Try passing the $config[per_page] in the query 尝试在查询中传递$ config [per_page]

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

And then in the query (notice we pass the per_page variable to the limit()) 然后在查询中(注意我们将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();
}

Hope this helps 希望这可以帮助

Altrim answer is very good. Altrim的回答非常好。 But to stay conform to CodeIgniter, I recommend using this instead : 但为了保持符合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();
}

You already define per_page in $config['per_page'] = 20; 你已经在$config['per_page'] = 20;定义了per_page $config['per_page'] = 20;

Since $this->pagination->initialize($config); 由于$this->pagination->initialize($config); transform $key (per_page) => $value (20) in $this->$key = $value in initialize function. $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