Codeigniter分页链接转到404 Page Not Found

时间:2013-10-01 12:35:01

标签: php codeigniter pagination

我已将此Paginate with search result实施到我的CI项目中,但是我无法将我的分页链接转到下一页,例如:

首次加载时,我的搜索页面为http://localhost/ci_project/search,当我点击将转到http://localhost/ci_project/search/2的分页链接时,页面会显示为404 Page Not Found

有人可以帮我弄清楚我的代码中有什么问题吗?我在解决方案上尝试了很多建议,但无法提供帮助,大部分问题来自$config['base_url'] = base_url('search');,但没有一个是帮助。

型号:

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;
    }
}

控制器:

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);
}

感谢。

4 个答案:

答案 0 :(得分:3)

尝试:

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

答案 1 :(得分:0)

codeigniter中的

http://localhost/ci_project/search/2可能正在尝试使用常规配置执行名为2的方法,如果您的方法为search,请务必在http://localhost/ci_project/search_controller/search/2调用网址。

如果由于某种原因想要更改配置,可以启动diggin here

答案 2 :(得分:0)

尝试

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

无需更改路由配置,这对我有用。

答案 3 :(得分:0)

尝试一下: 如果您在索引页上,则调用这种索引方法。

'base_url' => base_url('page/index'),