在Codigniter中将分页URL更改为域/产品?page = x

时间:2018-02-22 10:44:46

标签: codeigniter pagination

对于我的Codeigniter项目,我有以下分页代码:

$this->load->library('pagination');
$config['base_url'] = base_url().'/joblist';
$config['total_rows'] = $this->joblist_model->joblist_count();
$config['per_page'] = 2;
$config['use_page_numbers'] = TRUE;
$config["uri_segment"] = 2;

$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;

$data['title'] = 'Job list in Cambodia';
$data['rows'] = $this->joblist_model->joblist($config["per_page"], $page);

$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();

问题:如何将域/ joblist / page / xx中的URL更改为域/ joblist?page = xx?

2 个答案:

答案 0 :(得分:2)

是的,您可以在分页中启用查询字符串。

$this->load->library('pagination');
$config['base_url'] = base_url().'/joblist';
$config['total_rows'] = 4;
$config['per_page'] = 2;
$config['use_page_numbers'] = TRUE;
$config["uri_segment"] = 2;
$config['page_query_string']=TRUE;
$config['query_string_segment'] = 'page';

$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$this->pagination->initialize($config);
$this->pagination->create_links());

答案 1 :(得分:1)

以下是使用$this->input->get();并将page_query_string设置为true

的示例

您可能需要在config / routes.php上设置一些路由

https://www.codeigniter.com/user_guide/general/routing.html#examples

$route['product'] = 'joblist/index';

控制器示例

class Joblist extends CI_Controller {

    public function index() {

        $this->load->library('pagination');

        $config['base_url'] = base_url('product');
        $config['total_rows'] = $this->db->count_all_results('jobs');
        $config['per_page'] = 2;
        $config['page_query_string'] = TRUE;
        $config['query_string_segment'] = 'page';

        // Use the input->get();

        $page = ($this->input->get('page')) ? $this->input->get('page') : 0;

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

        $data['rows'] = $this->fetch_jobs($config['per_page'], $page);

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

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

    }

    public function fetch_jobs($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get("jobs");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

}