【问题标题】:Why do i get the same values in all pages in CI pagination?为什么我在 CI 分页的所有页面中得到相同的值?
【发布时间】:2019-02-02 10:41:17
【问题描述】:

我是 CI 的新手,我正在尝试为我的页面创建适当的分页。

我有以下模型和控制器,即使我设法获得分页链接,当我访问其他页面时,我也会一次又一次地获得相同的条目。

我还应用了一个 Ajax“GET”过滤来从数据库中获取结果,并且在我更改页面时我设法重用了 url 参数。

问题是我不知道是否应该尝试创建无限滚动功能或保持默认的 CI 分页。

我也想听听你的意见!

提前谢谢你!

public function get_filtered_pets($limit, $start){
 $this->db->order_by('pet_created_at', 'DESC');
 $this->db->join('users', 'users.user_id = pets.pet_user_id');
 $this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');

$pet_data = array();
if ( $this->input->get('petStatus') ) {
  $pet_data['pet_status'] = $this->input->get('petStatus');
}
if ( $this->input->get('petCategory') ) {
  $pet_data['category_id'] = $this->input->get('petCategory');
}
if ( $this->input->get('petGender') ) {
  $pet_data['pet_gender'] = $this->input->get('petGender');
}

if ( !empty($pet_data) ) {
  $this->db->where($pet_data);
}

$query = $this->db->get('pets');
if ($start > 0) {
  $this->db->last_query();
}

return $query->result_array();

}

还有我的控制器

public function index(){

$query = $this->db->get('pets');
$pet_num = $query->num_rows();

$config = array();

$config["base_url"] = base_url() . "pets/index/";
$config["total_rows"] = $pet_num;
$config["per_page"] = 12;
$config['use_page_numbers'] = TRUE;
$config['reuse_query_string'] = TRUE;

$config['attributes'] = array('class' => 'page-link');
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_link"] = "&laquo;";
$config["first_tag_open"] = "<li>";
$config["first_tag_close"] = "</li>";
$config["last_link"] = "&raquo;";
$config["last_tag_open"] = "<li>";
$config["last_tag_close"] = "</li>";
$config['next_link'] = '&gt;';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '<li>';
$config['prev_link'] = '&lt;';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '<li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';

$this->pagination->initialize($config);

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pets'] = $this->pet_model->get_filtered_pets($config["per_page"], $page);

$data["links"] = $this->pagination->create_links();

//View files
$this->load->view('templates/header', $data);
$this->load->view('pets/index', $data);
$this->load->view('templates/footer', $data);
}

【问题讨论】:

    标签: ajax codeigniter url pagination


    【解决方案1】:

    您没有在任何地方使用$limit$start 的值。见Limiting or Counting Results

    你应该:

    public function get_filtered_pets($limit, $start){
    
        $this->db->limit($limit, $start);
    
        /* ... */
    
        return $query->result_array();
    }
    

    而且,当调用这个函数时,你必须乘以:per_page * page,以获得偏移量(起始索引)。

    $this->pet_model->get_filtered_pets($config["per_page"], $page * $config["per_page"])
    

    为了正确计算结果,我通常在模型上有两个函数:“get($search, $offset)”和“count($search)”。

    函数countget 具有完全相同的代码,除了选择字段。

    看到这段代码,我想你会明白的:https://pastebin.com/dZG90Lzm

    ps:有办法优化它并避免重复代码。

    【讨论】:

    • 仍然没有.. 我做了这些更改,问题仍然存在。这是更改文件的 pastebin.. pastebin.com/7pGj9dnQ
    • @HarrysR。第2页的url是什么?
    • @HarrysR。把这个echo $this-&gt;db-&gt;last_query();放在return $query-&gt;result_array()之前
    • 那很奇怪..或者我在这里有点困惑..我得到 SELECT * FROM pets JOIN users ON users.user_id = pets.pet_user_id JOIN pet_categories ON pet_categories.pet_category_id = pets.category_id ORDER BY pet_id DESC LIMIT 24, 12 最后一个查询...
    • 嗯,我想我解决了(你可以在 pastebin 中再次检查),我认为我的计算是错误的,因为我使用了页码:stackoverflow.com/questions/9955394/…
    猜你喜欢
    • 2017-11-10
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多