Codeigniter pagination link go to 404 Page Not Fou

2019-07-07 11:44发布

问题:

I had this Paginate with search result implemented into my CI project, However I was unable to have my paginate links go to the next page, for example:

my search page when it first loaded is http://localhost/ci_project/search, when I clicked the paging link which will go to http://localhost/ci_project/search/2, the page is shown 404 Page Not Found.

Can someone please help me to figure out what's wrong in my code? I tried a lot of suggestion on the solution but can't help, most of the issue is came from $config['base_url'] = base_url('search');, but none of them are help.

Model:

public function do_search_count($keywords)
{
    $sql = "SELECT COUNT(*) AS cnt FROM products WHERE MATCH (name, manufacturer) AGAINST ('".$keywords."')";
    $q = $this->db->query($sql);
    $row = $q->row();
    return $row->cnt;
}

public function do_search($keywords, $per_page, $limit)
{
    $this->db->escape($keywords);

    $this->db->where('MATCH (name, manufacturer) AGAINST ("'.$keywords.'") LIMIT '.$per_page.', '.$limit, NULL, FALSE);
    $query = $this->db->get('products');

    if($query->num_rows() > 0){
        return $query->result();
    }else{
        return false;
    }
}

Controller:

function search()
{
    $keywords = $this->input->post('search');

    $searchterm = $this->db_model->searchterm_handler($this->input->get_post('search', TRUE));
    $limit = ($this->uri->segment(2) > 0)?$this->uri->segment(2):0;

    $config['base_url'] = base_url('search');
    $config['total_rows'] = $this->db_model->do_search_count($searchterm);
    $config['per_page'] = 5;
    $config['uri_segment'] = 2;
    $config['use_page_numbers'] = TRUE;
    $choice = $config['total_rows'] / $config['per_page'];
    $config['num_links'] = round($choice);  

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

    $data['results'] = $this->db_model->do_search(trim($keywords), $limit, $config['per_page']);
    $data['pagination'] = $this->pagination->create_links();
    $data['searchterm'] = $searchterm;

    $data['total'] = count($data['results']);
    $data['title'] = 'Search';

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

Thanks.

回答1:

Try:

$config['base_url'] = base_url('controller_name/search');
$config['uri_segment'] = 3;


回答2:

http://localhost/ci_project/search/2 in codeigniter is probably trying to execute a method called 2 with the usual configuration, if your method is search be sure to call the url at http://localhost/ci_project/search_controller/search/2.

If you want to change the configuration for some reason, you can start diggin here



回答3:

Try this

$config['base_url'] = base_url('index.php/controller_name/method_name');

No need change routes config, this works for me.