奇特的bug?分页问题,请求帮忙
本帖最后由 kopa 于 2017-5-24 12:34 编辑目前遇到的问题是,
请求 page = 1 的时候,程序把内容全部列出来了
请求 page = 2 的时候,程序就能正常读取分页数据。
controller 代码如下
$current_page = $this->input->post('page');
$current_page = $current_page ==0 ? 1 : $current_page ;
$numRows = 3;
$offset = ($current_page - 1 ) * $numRows; //设置偏移量 限定 数据查询 起始位置(从 $offset 条开始)
/*
分页查询格式
--- $table,$where,$offset,$num,$order_by
*/
$indexListData = $this->General->getPage('coupo',array('status'=>0),$offset,$numRows,'DESC');
model代码如下
public function getPage($table,$where,$offset,$num,$order_by)
{
$this->db->where($where);
$this->db->limit($offset,$num);
$this->db->order_by('dates', $order_by);
$query = $this->db->get($table);
return $query->result_array();
}
发现的状况如下
当page = 1,sql语句是这样的
SELECT *FROM `coupo` WHERE `status` =0 ORDER BY `dates` DESC
当page=2,sql语句是这样的
SELECT *
FROM `coupo`WHERE `status` =0 ORDER BY `dates` DESC LIMIT 3, 3
请教 问题出在哪
$this->db->limit() 的第一个参数是要取多少行,第二个参数是偏移量。这个整好和 SQL limit 是相反的。 难怪!!!谢谢老大 请教各位,设置好了分页类的,可是点击每个页码,总是显示一样的总数据条数的,一点也没改变的,到底哪里出了错?附上代码:
public function index() {
//后台设置后缀为空,否则分页出错
$this->config->set_item('url_suffix', '');
// 载入分页类
$this->load->library('pagination');
// 自定义每页显示条数
$pageCount = 1;
// 配置分页指向的url
$config['base_url'] = site_url('admin/register/index');
// 数据总条数
$config['total_rows'] = $this->register_model->getAllData();
// 每页显示条数
$config['per_page'] = $pageCount;
// url中包含页数的段
$config['uri_segment'] = 4;
// 载入配置项
$this->pagination->initialize($config);
// 创建分页
$data['links'] = $this->pagination->create_links();
$offset = $this->uri->segment(4);
$this->register_model->setLimit($pageCount, $offset);
$data['job'] = $this->register_model->get();
$this->load->view('admin/alljob.html', $data);
}
页:
[1]