在codeigniter中如何对搜索值进行分页

时间:2017-02-06 12:29:29

标签: php codeigniter pagination

列表页面

 public function index(){
        $searchParam = '';
        if(! empty($this->input->get('q'))){
            $searchParam = $this->input->get('q');
        }
        if(!empty($this->input->post('cityId'))){
            $search_city= $this->input->post('cityId');
        }
        $data['title'] = 'Area';
        $data['action'] = 'admin/area/';
        $data['searchParam'] = $searchParam;
        $data['arealist'] = $this->getList();
        $this->load->view('admin/header',$data);
        $this->load->view('admin/area/view.php', $data);
        $this->load->view('admin/footer',$data);
    }

    protected function getList(){
        $stateId = $this->session->userdata('stateId');
        $searchParam = '';
        $search_city = '';
        if(! empty($this->input->get('q'))){
            $searchParam = $this->input->get('q');
        }
        if(!empty($this->input->post('cityId'))){
            $search_city= $this->input->post('cityId');
        }
        if(!empty($this->input->post('limit'))){
            $limit= $this->input->post('limit');
        }
        else
        {
            $limit = 10;
        }
         $config = $this->config->item('pagination');  
        $config = array();
        $config["base_url"] = base_url() . "admin/area/index/";
        $config["total_rows"] = $this->area_model->getArea(0, 0, $searchParam, 1,$search_city);
        $totalCount = $config['total_rows'];
        $config["per_page"] = $limit;
        $config["uri_segment"] = 4;
        $config['use_page_numbers']  = TRUE;
        $config['reuse_query_string'] = TRUE;
        $data['cityId'] = $search_city;
        $data['limit_selected'] = $limit;
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 1;              
        $data["links"] = $this->pagination->create_links();
        $data["areaList"] = $this->area_model->getArea($config["per_page"], $page, $searchParam,0,$search_city);
        $data['cityOptionList'] = $this->area_model->getCityByStateId($stateId);    
        return $this->load->view('admin/area/list.php', $data, true);

    } 

控制器代码

float Employee::getSalary(){

    return salary;

}

在列表页面中,当我更改城市时,数据将根据城市获取并显示在列表页面中,如果有多个选定记录,则显示分页。 问题是当我点击搜索值的分页链接时,所有记录都加载而不是搜索值。 在分页中,仅当搜索应用于记录时,如何获取搜索值。

2 个答案:

答案 0 :(得分:0)

抱歉错过了你的整个问题。要将您的搜索参数作为分页中的过滤器,您需要添加它们。请参阅下面的链接,查看您需要更改的配置中的设置。

https://www.codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination

您需要更改以下设置。

 $config[‘reuse_query_string’] = FALSE;

我猜你的代码中的搜索表单会在用户提交时执行POST操作。用户提交表单后,当然会有效。因为他们刚刚发出POST请求,并且您使用$ this-> input-> post('user_input')或类似内容从$ _POST获取搜索查询。但是,当他们尝试通过单击页码链接(这是一个GET请求)导航到另一个页面时,搜索失败,因为超全局$ _POST数组为空。

解决此问题的解决方案是根据用户输入设置会话值(我建议您使用CodeIgniter的会话类),而不是使用$ _POST中的搜索查询,而是使用存储在会话中的会话值。

答案 1 :(得分:0)

根据您的要求和您的表格尝试此更改代码

以这种方式设置控制器

       <?php
class pagination extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->database();
        $this->load->library('pagination');
        $this->load->model('pagination_model');
    }

    public function index()
    {
        //pagination settings
        $config['base_url'] = site_url('pagination/index');
        $config['total_rows'] = $this->db->count_all('tbl_books');
        $config['per_page'] = "3";
        $config["uri_segment"] = 3;
        $choice = $config["total_rows"]/$config["per_page"];
        $config["num_links"] = floor($choice);

        // integrate bootstrap pagination
        $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);

        $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        // get books list
        $data['booklist'] = $this->pagination_model->get_books($config["per_page"], $data['page'], NULL);

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

        // load view
        $this->load->view('pagination_view',$data);
    }

    function search()
    {
        // get search string
        $search = ($this->input->post("book_name"))? $this->input->post("book_name") : "NIL";

        $search = ($this->uri->segment(3)) ? $this->uri->segment(3) : $search;

        // pagination settings
        $config = array();
        $config['base_url'] = site_url("pagination/search/$search");
        $config['total_rows'] = $this->pagination_model->get_books_count($search);
        $config['per_page'] = "5";
        $config["uri_segment"] = 4;
        $choice = $config["total_rows"]/$config["per_page"];
        $config["num_links"] = floor($choice);

        // integrate bootstrap pagination
        $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'] = 'Prev';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'Next';
        $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);

        $data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        // get books list
        $data['booklist'] = $this->pagination_model->get_books($config['per_page'], $data['page'], $search);

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

        //Load view
        $this->load->view('pagination_view',$data);
    }
}
?>

以这种方式设置模型

       <?php
class pagination_model extends CI_Model{

    function __construct()
    {
        parent::__construct();
    }

    //fetch books
    function get_books($limit, $start, $st = NULL)
    {
        if ($st == "NIL") $st = "";
        $sql = "select * from tbl_books where name like '%$st%' limit " . $start . ", " . $limit;
        $query = $this->db->query($sql);
        return $query->result();
    }

    function get_books_count($st = NULL)
    {
        if ($st == "NIL") $st = "";
        $sql = "select * from tbl_books where name like '%$st%'";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }
}
?>

然后您的视图页面看起来像

 <!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CodeIgniter Pagination Example with Search Query Filter</title>
    <link rel="stylesheet" href="<?php echo base_url("bootstrap/css/bootstrap.css"); ?>">

    <style type="text/css">
    .bg-border {
        border: 1px solid #ddd;
        border-radius: 4px 4px;
        padding: 15px 15px;
    }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2 well">
        <?php 
        $attr = array("class" => "form-horizontal", "role" => "form", "id" => "form1", "name" => "form1");
        echo form_open("pagination/search", $attr);?>
            <div class="form-group">
                <div class="col-md-6">
                    <input class="form-control" id="book_name" name="book_name" placeholder="Search for Book Name..." type="text" value="<?php echo set_value('book_name'); ?>" />
                </div>
                <div class="col-md-6">
                    <input id="btn_search" name="btn_search" type="submit" class="btn btn-danger" value="Search" />
                    <a href="<?php echo base_url(). "index.php/pagination/index"; ?>" class="btn btn-primary">Show All</a>
                </div>
            </div>
        <?php echo form_close(); ?>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8 col-md-offset-2 bg-border">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                    <th>#</th>
                    <th>Book Name</th>
                    <th>Author Name</th>
                    <th>ISBN</th>
                    </tr>
                </thead>
                <tbody>
                <?php for ($i = 0; $i < count($booklist); ++$i) { ?>
                <tr>
                    <td><?php echo ($page+$i+1); ?></td>
                    <td><?php echo $booklist[$i]->name; ?></td>
                    <td><?php echo $booklist[$i]->author; ?></td>
                    <td><?php echo $booklist[$i]->isbn; ?></td>
                </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <?php echo $pagination; ?>
        </div>
    </div>
</div>
</body>
</html>