【问题标题】:Codeigniter pagination offset issueCodeigniter 分页偏移问题
【发布时间】:2017-06-15 10:36:05
【问题描述】:

我的看法:

 <?php echo $this->pagination->create_links();?>

我的控制器:

public function __construct()
{
    parent:: __construct();
    $this->load->model('admin_products_model');
    $this->load->library('pagination');
}

function index()
{   
    $config['base_url'] = base_url().'products/index/';
    $config['total_rows']=$this->db->get('products')->num_rows();
    $config['per_page']=10;
    $config['num_links']=10;
    $config['use_page_numbers'] = TRUE;

    $this->pagination->initialize($config);

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data['products']= $this->admin_products_model->getProducts($config['per_page'],$page); 

    $this->load->view('products',$data);
}

我的模特:

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 个组升序排列。例如,第一页:85-95。第 2 页:86-96。等等。 而不是 85-95、96-106。

【问题讨论】:

  • 我不明白您为什么要获取 num_rows,而只是 return $query-&gt;result();。您在 codeigniter 中不必要地推送核心 php。
  • 您还需要检查记录是否存在 - 并检查 $this-&gt;uri-&gt;segment(3) 返回您想要的数字。

标签: codeigniter pagination


【解决方案1】:

控制器功能:

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-&gt;db-&gt;limit($page, $offset);中提及它们

【讨论】:

  • 非常感谢。我还删除了: $config['use_page_numbers'] = TRUE;行,它奏效了!
  • @marilena6 : 如果对你有用,请接受答案
猜你喜欢
  • 2016-03-16
  • 2013-09-17
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 2015-04-27
相关资源
最近更新 更多