笨分页不渲染分页链接(Codeigniter pagination not rendering pa

2019-09-17 01:27发布

你好,我有以下代码,

$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结果,但我没有得到任何联系可言,我会做一些错了吗?

Answer 1:

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

希望这可以帮助



Answer 2:

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

你已经定义per_page $config['per_page'] = 20;

由于$this->pagination->initialize($config); 变换$key (per_page) => $value (20)$this->$key = $valueinitialize功能。



文章来源: Codeigniter pagination not rendering pagination links