Codeigniter分页不呈现分页链接 [英] Codeigniter pagination not rendering pagination links

查看:95
本文介绍了Codeigniter分页不呈现分页链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有以下代码

$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))运行的查询看起来像这样

The query this is being run by get_products_and_category($this->uri->segment(4)) looks like this,

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个结果,第二页应在其中显示20个结果应该有4个结果,但是我根本没有任何链接,我做错了吗?

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?

推荐答案

LIMIT子句可用于约束SELECT语句返回的行数.

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement.

现在您有25个结果,并且您将查询限制为返回25个结果,因此分页可能无法正常工作.

Now you have 25 results and you limit your query to return 25 results so your pagination might not work correctly.

尝试在查询中传递$ config [per_page]

Try passing the $config[per_page] in the query

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

然后在查询中(注意,我们将per_page变量传递给limit())

And then in the query (notice we pass the per_page variable to the 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();
}

希望这会有所帮助

这篇关于Codeigniter分页不呈现分页链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆