笨返回错误的“分页”结果
问题描述:
我有工作应该是为了获取它的控制器准备的拼版数据这个辅助方法...... 基本上,这是发生在助手笨返回错误的“分页”结果
if (!isset($_GET['page'])) {
$_GET['page'] = 1;
}
if (!isset($_GET['per_page'])) {
$_GET['per_page'] = 5;
}
$results = $ci->$model->get('', $_GET['per_page'], $_GET['page']);
代码这是我的模型,它应该返回数据
public function get($tableName = "", $limit = null, $start = null)
{
if ($tableName == "") {
$tableName = $this->table;
}
if ($limit != null && $start != null) {
// problem is here with limit and start which returns wrong data
$this->db->limit($limit, $start);
$query = $this->db->get($tableName);
var_dump($query->result());
die();
}
} else {
$query = $this->db->get($tableName);
}
return $query->result();
}
问题是,从模型返回的数据是不正确的,我无法弄清楚如何正确获取基于页码和每页项目数据... 。 所以在我的情况下,如果我请求数据与paramas $_GET['page'] = 1
和$_GET['per_page'] = 5
它将返回5条记录,但从记录2开始,直到记录6.所以我的问题是如何正确请求给我说5条记录在第一页上,然后给我另外5条记录第2页ETC ...
如果您需要任何其他信息,请让我知道,我会提供。谢谢
答
问题就出在你的$开始的变量。你应该记住,使用获得的第一个5个记录时,你应该使用一个偏移量为0而不是1开始计数从0记得:)
的代码应该是
public function get($tableName = "", $limit = null, $start = null)
{
if ($tableName == "") {
$tableName = $this->table;
}
if ($limit != null && $start != null) {
// problem is here with limit and start which returns wrong data
//$this->db->limit($limit, $start);
// use this instead
$this->db->limit($limit, ($start - 1) * $limit);
$query = $this->db->get($tableName);
var_dump($query->result());
die();
}
} else {
$query = $this->db->get($tableName);
}
return $query->result();
}
Yeaa,但这意味着我(页面= 0&per_page = 3,page = 3&per_page = 3,page = 6&per_page = 3) –
对不起仍然不好(页面= 1&per_page = 3)返回结果开始与3 –
我编辑了代码。为params函数的$ start放置一个默认值。 – redfades