在codeigniter中使用分页搜索

时间:2017-07-15 19:06:22

标签: php codeigniter search pagination

您好我正在尝试使用Code Igniter中的分页进行搜索,以便在第一页上正常工作,但是当用户尝试单击指向下一页的链接时,它会显示大于实际搜索结果的分页编号。加上数据没有正确显示。

我的控制器:

public function search() {
    try {

        $data = new stdClass();

        $name = $this->input->post('name');

        $config["base_url"] = base_url() . "gadmin_user/search";
        $config["total_rows"] = $this->Gadmin_model->search_count($name);
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = round($choice);
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $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>';
        $config['prev_link'] = '<li>&lt;';
        $config['next_tag_open'] = '<li>';


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

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data->users = $this->Gadmin_model->serach_by_user($name,$config["per_page"], $page);
        $data->links = $this->pagination->create_links();

        $this->load->view('gadmin/templates/header');
        $this->load->view('gadmin/user/search', $data);
        $this->load->view('gadmin/templates/footer');

    } catch (Exception $e) {
        show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
    }
}

我的模特:

public function search_count($keyword){


    $q =  $this->db->select('*')
                        ->from('users')
                        ->where("name LIKE '%$keyword%'")
                        ->get();

    return $q -> num_rows();

}


public function serach_by_user($keyword,$start, $limit){

    $search =  $this->db->select('*')
                        ->from('users')
                        ->where("name LIKE '%$keyword%'")
                        ->limit($start, $limit)
                        ->get();

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

}

1 个答案:

答案 0 :(得分:1)

要获得限制,请使用此限制,限制为$ config [&#34; per_page&#34;]

  $c=1;
        if($page){
            $c = $page;
            $limit_start = $limit * ($c - 1);
        }