Codeigniter分页偏移问题
我的看法是:Codeigniter分页偏移问题,codeigniter,pagination,Codeigniter,Pagination,我的看法是: <?php echo $this->pagination->create_links();?> 我的模型: function getProducts($limit,$offset) { $this->db->limit($limit); $this->db->offset($offset); $query = $this->db->get('products'); if($query-
<?php echo $this->pagination->create_links();?>
我的模型:
function getProducts($limit,$offset)
{
$this->db->limit($limit);
$this->db->offset($offset);
$query = $this->db->get('products');
if($query->num_rows()>0)
{
foreach($query->result() as $row){
$products[]=$row;
}
return $products;
}
}
因此,当我点击导航链接时,结果ID不会以10为一组递增。例如第1页:85-95。第二页:86-96。等
而不是85-95、96-106。控制器功能:
public function index()
{
$config ['base_url'] = base_url().'products/index/';
$config ['total_rows'] = $this->db->get('product')->num_rows();
$config ['uri_segment'] = 3;
$config ['per_page'] = 10;
$config ['num_links'] = 10;
$this->pagination->initialize($config);
$page = $config ['per_page']; // how many number of records on page
$segment = $this->uri->segment ( 3 ); // from which index I have to count $page number of records
$data['products']= $this->products_model->getProducts($page, $segment);
$this->load->view('products', $data);
}
模型函数
public function getProducts($page, $segment)
{
$this->db->select("prid");
$this->db->limit($page, $segment);
$query = $this->db->get('product');
return $query->result();
}
视图:
<?php
echo "<pre>";
print_r($products);
echo "</pre>";
?>
<br>
<br>
<?php echo $this->pagination->create_links();?>
您的代码中缺少了
$page
和$offset
内容。您不必在模型中单独传递它们,在$this->db->limit($page,$offset)中提及它们
我不明白为什么要获取num_行,只需返回$query->result()代码>取而代之。您在codeigniter中不必要地推送了核心php。您还需要检查记录是否存在-并检查$this->uri->segment(3)
返回您想要的数字。非常感谢。我还删除了:$config['use\u page\u numbers']=TRUE;线和它的工作@玛丽莲6:如果这个答案对你有用,请接受
<?php
echo "<pre>";
print_r($products);
echo "</pre>";
?>
<br>
<br>
<?php echo $this->pagination->create_links();?>