php – 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))运行的查询如下所示,

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

希望这可以帮助

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...