2012-05-04 128 views
4

你好,我有以下代码,笨分页不渲染分页链接

$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个结果,但是我没有收到所有的链接,我做错了什么?

+1

我假设你在模板中回显数据['分页'],对吧? – IsisCode

+0

Absolutley - 那对我来说真的很愚蠢 – Udders

+2

@ sico87,有时候人们犯了愚蠢的错误,总是值得提问,尤其是因为我们没有看到你的代码。 – Jakub

回答

4

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

希望这有助于

+1

我不相信会影响分页创作。分页库正在传递总共有25行的参数,并且20个项目将被显示在一页中。因此,无论查询结果如何,它都应该生成2个锚。该库与查询结果没有任何关系。 – Broncha

0

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

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

定义per_page由于$this->pagination->initialize($config);initialize函数变换$key (per_page) => $value (20)$this->$key = $value