【问题标题】:In codeigniter how to do pagination on searched values在codeigniter中如何对搜索值进行分页
【发布时间】:2017-02-06 12:29:28
【问题描述】:

列表页

<div class="container">
<!--search criteria-->
<div class="alert alert-info">
<form method="post" class="form-inline" action="<?php echo base_url();?>/admin/area/index" id="form_search">
        <div class="pull-left" style="color:#666;">Show Results:&nbsp;&nbsp;
    <select name="limit" id="limit" style="width:50px;">
          <option value="10"<?php if(10==$limit_selected) echo "selected";?>>10</option>
          <option value="20" <?php if(20==$limit_selected) echo "selected";?>>20</option>
          <option value="30" <?php if(30==$limit_selected) echo "selected";?>>30</option>
          <option value="40" <?php if(40==$limit_selected) echo "selected";?>>40</option>
          <option value="50" <?php if(50==$limit_selected) echo "selected";?>>50</option>
        </select>
    </div>
<label style="color:#666;">&nbsp;&nbsp;Search By City:&nbsp;&nbsp;</label>
<select id="cityId" name="cityId" >
               <option value='0'> --All-- </option>
                   <?php 
                  foreach($cityOptionList as $city) {  ?>
                    <option value="<?php echo $city['id']?>" <?php if($city['id']==$cityId) echo "selected";?>><?php echo $city['name']?></option>
                  <?php } ?> 
              </select>
</form>
</div>
<!--search criteria end-->
    <table class="table table-striped table-bordered bootstrap-datatable">
    <thead>
        <tr>
            <th>Sl.no</th>
            <th>Area Name</th>
            <th>City</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php $i=1; foreach($areaList as $area) {?>
        <tr>
            <td><?php echo $i;?></td>
            <td class="center"><?php echo $area->areaName;?></td>
            <td class="center"><?php echo $area->city_name;?></td>
            <td class="center">
            <a  href="<?php echo base_url();?>admin/area/edit/<?php echo $area->areaId ; ?>">
            <i class="icon-edit" style="font-size:16px;"></i></a>
        <!--    <a  href="#" data-id="<?php echo $area->areaId;?>">
                <i class="icon-trash" style="font-size:16px;"></i></a>-->
            </td>
        </tr>
        <?php $i++; } ?>
    </tbody>
</table>   
<?php echo $links;?>            
</div>
<!--/container--> 
<!--=== End Content Part ===-->
<script>
$(function() {
   $("#cityId").change(function() {
     $("#form_search").submit();
   });

    $("#limit").change(function() {
     $("#form_search").submit();
   });
  /* 
   $(".pagination").click(function(){
          alert("hi");
         $("#form_search").submit();
   })
   */
 });
</script>

控制器代码

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

    } 

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

【问题讨论】:

  • 离题:更短的方法: is = ($limit_selected == 20 ? 'selected' :'') ?>.因为, 与 = $some ?> 的含义相同。 ;)

标签: php codeigniter pagination


【解决方案1】:

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

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 的搜索查询,而是使用存储在会话中的那个。

【讨论】:

    【解决方案2】:

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

    这样设置控制器

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

    那么你的 veiw 页面看起来像

     <!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>
    

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 1970-01-01
      • 2016-01-30
      • 2021-05-22
      • 2016-06-13
      • 2021-01-02
      • 2014-12-14
      • 1970-01-01
      相关资源
      最近更新 更多