我在使用分页时遇到了一些问题。 这是我的模型代码:
public function count_locations() {
return $this->db->count_all("locations");
}
public function fetch_locations($limit, $start) {
$this->db->select('locations.id as location_id, locations.name_ka, locations.description_ka, locations.recomendation, locations.img, locations.nature, locations.culture, locations.resort, regions.region_name_ru, regions.region_type, resort.resort_name_ru, nature.nature_name_ru, nature.nature_name_en, culture.culture_name_ru, culture.culture_lat')
->from('locations')
->join('regions', 'regions.id = locations.region','left')
->join('culture', 'culture.id = locations.culture','left')
->join('resort', 'resort.id = locations.resort','left')
->join('nature', 'nature.id = locations.nature','left');
$this->db->order_by('location_id','asc');
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result_array();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
这是我的控制器:
function page(){
$this->load->view('templates/head');
$this->load->view('templates/header');
$config['base_url'] = base_url() . 'location/page';
$config['total_rows'] = $this->Location_model->count_locations();
$config['per_page'] = 4;
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Location_model->
fetch_locations($config["per_page"], $page);
$this->load->view('location/location',$data);
$data["links"] = $this->pagination->create_links();
$this->load->view("location/pagination", $data);
$this->load->model('Footer');
$dataf['links'] = $this->Footer->get_links();
$this->load->view('templates/footer',$dataf);
$this->load->view('templates/end');
}
查看文件内容:
$object = array();
foreach($results as $data) {
echo $data['location_id'] . "<br>";
}
echo $links;
结果它每页给我4个元素。没关系。但是转到下一页时会出现问题。第一页显示:56, 57, 58, 59
。第二页:58, 59, 60, 61
。第三页:60, 61, 62, 63
。
结果必须为:首页:56, 57, 58, 59
,第二页:60, 61, 62, 63
,第三页:64, 65, 66, 67
。
答案 0 :(得分:0)
你的$page
在这里错了。你在这里使用分段。
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
这应该如下
if ($this->input->get('page')) {
$sgm = (int) trim($this->input->get('page'));
$page = $config['per_page'] * ($sgm - 1);
} else {
$page= 0;
}
现在
$data["results"] = $this->Location_model->fetch_locations($config["per_page"], $page);
答案 1 :(得分:0)
我的第一个猜测是,您在页面上发布的项目序列并不完全一样。 我猜你见过: 第一名:56,57,58,59 第二名:58,59,60,61 第三名:59,60,61,62 但是没有仔细阅读它。
这就是为什么......你用过:
$config['use_page_numbers'] = TRUE;
检查它的作用https://ellislab.com/codeigniter/user-guide/libraries/pagination.html告诉您这使得uri段成为页码而不是之前页面上的第一个项目偏移量。
这只是第一次猜测。 我建议学习一些基本的调试方法。 例如var_dumping某些关键点上的一些参数可以帮助您更快地找到错误,而不是等待stackoverflow上的响应。
提示:在我们代码的大致中间找到关键点,并检查是否传递了正确的参数。根据结果,你可以在我们代码的一半或另一半中进行修改。
这里:例如,您可以var_dump模型中的参数,以了解它是DB /模型中的错误,还是从视图/控制器中获取错误的参数。
除非你学会了这一点,否则你将无法享受有趣的编程,并且整天陷入沮丧的时刻。