为什么在CI分页的所有页面中都得到相同的值?

时间:2018-08-27 21:08:47

标签: ajax codeigniter url pagination

我对CI有点陌生,我正在尝试为我的页面创建适当的分页。

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

我还应用了Ajax“ GET”过滤器来从db获取结果,并且在更改页面时设法重用了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);
}

1 个答案:

答案 0 :(得分:0)

您没有在任何地方使用$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:有一些方法可以对其进行优化并避免重复的代码。