【问题标题】:Codeigniter pagination duplicating results on pagesCodeigniter分页在页面上复制结果
【发布时间】:2016-03-16 14:43:42
【问题描述】:

我在使用分页时遇到了一些问题。 这是我的模型代码:

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

【问题讨论】:

    标签: php codeigniter pagination


    【解决方案1】:

    我的第一个猜测是,您在页面上发布的项目顺序与实际情况并不完全相同。 我假设你已经看到: 第一: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 以了解它是否在数据库/模型中出错,或者您从视图/控制器中获取了错误的参数。

    除非你学会了这一点,否则你将没有乐趣编程并且整天陷入沮丧的时刻。

    【讨论】:

      【解决方案2】:

      您的$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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-07
        • 2014-03-26
        • 2014-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多