首先,我是CodeIgniter的新手,我已经搜索并发现了很多类似的问题,我已经尝试了所有的解决方案,但都没有。我有一个搜索输入,用分页显示我的数据,问题出现在第二页的网址中设置了获取参数,显示的数据是第二页,但分页号链接仍在第1页。我将只显示相关代码(我已正确放置构造,索引,载荷等)。
模型/ model.php
public function get_results($search_term='default', $limit, $start) {
$this->db->select('*');
$this->db->from('table');
$this->db->like('title', $search_term');
$this->db->order_by('PID DESC');
$query = $this->db->get('', $limit, $start);
return $query->result();
}
控制器/ Controller.php这样
public function execute_search() {
// Retrieve the posted search term
$search_term = $this->input->post('search');
// Retrieve get variables if goes to second page
// url example: localhost:/index/main/execute_search/query/15
// If segment is 4, the third segment is the query get variable passed through link
$search_term = ($this->uri->segment(4)) ? $this->uri->segment(3) : $search_term;
// Pagination settings
$config['base_url'] = site_url('main/execute_search/' . $search_term . '/');
$this->db->like('title', $search_term);
$this->db->from('table');
$totalrows = $this->db->count_all_results();
$config['total_rows'] = $totalrows;
$config['per_page'] = '15';
$config['uri_segment'] = 3;
$choice = $config['total_rows'] / $config['per_page'];
$config['num_links'] = floor($choice);
// config for bootstrap pagination class integration
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
// If segment is 4, current page is the fourth segment
$data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
// Use a model to retrieve the results
$data['datalist'] = $this->model->get_results($search_term, $config['per_page'], $data['page']);
$data['pagination'] = $this->pagination->create_links();
// Pass the results to the view
$this->load->view('main', $data);
视图/ main.php
<!DOCTYPE HTML>
<html>
<head>
<!-- Bootstrap css -->
<link rel='stylesheet' href='<?= base_url('assets/css/bootstrap.css'); ?>'></link>
</head>
<body>
<?php
echo form_open('main/execute_search');
echo form_input(array('name'=>'search'));
echo form_submit('search_submit', 'Search');
// result
for($i = 0; $i < count($datalist); $i++) {
echo $datalist[$i]->title;
}
// Pagination
echo $pagination
?>
</body>
</html>
本地主机/索引/主/ execute_search
本地主机/索引/主/ execute_search / 15
本地主机/索引/主/ execute_search /查询/ 15
本地主机/索引/主/ execute_search /查询/ 30
当我在网址中添加了获取参数时出现问题,请帮我解决这个问题。非常感谢你。
答案 0 :(得分:1)
移动:
$data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
上图:
$config['base_url'] = site_url('main/execute_search/' . $search_term . '/');
然后改变:
$config['base_url'] = site_url('main/execute_search/' . $search_term . '/');
到
$config['base_url'] = site_url('main/execute_search/'.$data['page'].'/' . $search_term . '/');
同样改变:
$search_term = ($this->uri->segment(4)) ? $this->uri->segment(3) : $search_term;
到
$search_term = ($this->uri->segment(5)) ? $this->uri->segment(5) : $search_term;