Php Codeigniter分页在页面上复制结果
我在使用分页时遇到一些问题。 以下是我的模型代码:Php Codeigniter分页在页面上复制结果,php,codeigniter,pagination,Php,Codeigniter,Pagination,我在使用分页时遇到一些问题。 以下是我的模型代码: 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, locatio
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;
$object=array();
foreach($结果作为$数据){
echo$data['location_id']。“
”;
}
echo$链接;
因此,它为我提供了每页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
您的$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;
}
现在
您的
$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;
}
现在
我的第一个猜测是,您在页面上发布的项目顺序与实际情况不完全相同。 我想你已经看到了: 第一名:56、57、58、59 第二名:58、59、60、61 第三名:59,60,61,62 但是我们没有仔细阅读 这就是为什么。。。您使用了:
$config['use_page_numbers'] = TRUE;
检查一下它所做的事情就会告诉您,这会使uri段成为页码,而不是页面上的第一个项目偏移量
这只是第一个猜测。
我建议学习一些基本的调试方法。
例如,var_在一些关键点上转储一些参数将帮助您更快地发现错误,而不是等待stackoverflow上的响应
提示:在代码的大致中间找到关键点,并检查是否传递了正确的参数。根据结果,您可以继续插入我们的一个或另一半代码
这里:例如,您可以var_转储模型中的参数,以了解这是DB/model中的错误,还是您从view/controller获取了错误的参数
除非你学会了这一点,否则你的编程将毫无乐趣,整天都会陷入沮丧的时刻。我的第一个猜测是,你在页面上发布的项目顺序与实际不完全一样。 我想你已经看到了: 第一名:56、57、58、59 第二名:58、59、60、61 第三名:59,60,61,62 但是我们没有仔细阅读 这就是为什么。。。您使用了:
$config['use_page_numbers'] = TRUE;
检查一下它所做的事情就会告诉您,这会使uri段成为页码,而不是页面上的第一个项目偏移量
这只是第一个猜测。
我建议学习一些基本的调试方法。
例如,var_在一些关键点上转储一些参数将帮助您更快地发现错误,而不是等待stackoverflow上的响应
提示:在代码的大致中间找到关键点,并检查是否传递了正确的参数。根据结果,您可以继续插入我们的一个或另一半代码
这里:例如,您可以var_转储模型中的参数,以了解这是DB/model中的错误,还是您从view/controller获取了错误的参数
除非你学会了这一点,否则你就不会有有趣的编程,整天陷入沮丧的时刻