Codeigniter分页结果显示超过每页选定项目

时间:2016-01-19 21:24:04

标签: php codeigniter codeigniter-3

我是Codeigniter的新手并在这里寻求帮助。

我正在制作一个带分页的测试应用程序。我选择了$config['per_page'] = 1;,但不是在首页上显示一个列表,而是仍然显示所有这些列表。

我的控制器:

public function pagination(){
$keyword = $this->input->post('search[1]');
$main_keyword = $this->search_model->get_city_id_by_input($keyword);

    $config['base_url'] = base_url().'search/search-results/';
    $config['total_rows'] = $this->search_model->total_number_of_rows($main_keyword);
    $config['per_page'] = 1;
    $config['use_page_numbers'] = TRUE;
    $config['num_links'] = $this->search_model->total_number_of_rows($main_keyword);
    $config['cur_tag_open'] = ' ';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Previous';

$this->pagination->initialize($config);
$data['results'] = $this->search_model->get_search_results($main_keyword, $config['per_page'], $this->uri->segment(3));

    $this->load->view('view',$data);
}

型号:

function get_city_id_by_input($keyword){
    $this->db->select('id');
    $this->db->from('vbc_city');
    $this->db->where('v_city_name', $keyword);
    $query = $this->db->get();
    $query_result = $query->result_array();
    return $query_result();
}
  public function total_number_of_rows($main_keyword) {
      $this->db->select('vbc_item_id');
      $this->db->from('vbc_vacation_item_attri');
      $this->db->where('v_item_city', $main_keyword);
      $query = $this->db->get();
      return $query->num_rows();
}
  public function get_search_results($main_keyword) {
      $this->db->select('*');
      $this->db->from('vbc_vacation_item_attri');
      $this->db->where('v_item_city', $main_keyword);
      $query = $this->db->get();
      $result = $query->result();
      return $result;
    }

请帮忙。

1 个答案:

答案 0 :(得分:1)

比较你对函数的调用

$this->search_model->get_search_results($main_keyword, $config['per_page'], $this->uri->segment(3));

的签名
get_search_results($main_keyword)

当然你会得到所有结果。

我想你应该把你的功能转换成这样的东西

public function get_search_results($main_keyword, $perpage, $offset) {
  $this->db->select('*');
  $this->db->from('vbc_vacation_item_attri');
  $this->db->where('v_item_city', $main_keyword);
  $this->db->limit($perpage, $offset);
  $query = $this->db->get();
  $result = $query->result();
  return $result;
}