Codeigniter使用分页类

时间:2017-07-17 12:09:37

标签: codeigniter pagination

我正在尝试使用codeigniter构建具有分页功能的搜索功能。问题是我无法将搜索查询放在页面编号前面的Url上,以便我使用控制器中的get方法访问它。如何让网址显示如下:

http://localhost/project/shop/2/search?search_query=bolts

2是分页类用于标识页码的uri段。下面是我的控制器中的分页类配置的一部分:

$search_query=$this->input->get('search_query');

 $config['base_url'] = base_url().'shop/search?search='.$search_query;
 $config['total_rows'] = $this->ProductModel->countallByTerm($search query);
 $config['per_page'] = 2;

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

 $data['products']=$this->ProductModel->search($search_query, $config['per_page'], $start);
 $this->pagination->initialize($config);

当我点击我的分页链接上的“下一页”时,它会生成以下网址:

 http://localhost/project/shop/search?search=bolts/2

这是我在视图中的html代码,其中包含负责提交搜索查询的表单:

<form class="" action="<?php echo site_url('shop/search') ?>" method="get">
        <div class="input-field col md10 s10">
          <i class="material-icons prefix">search</i>
          <input type="text" name="search" class="validate" required>
          <label for="search">Search Products</label>
        </div>
        <div class="col md2 s2">
          <input type="submit" class="btn" value="search" name="submit" value="">
        </div>
      </form>

请帮忙

1 个答案:

答案 0 :(得分:1)

按如下方式更改表格:

<form class="" action="<?php echo site_url('shop/search') ?>" method="get">
        <div class="input-field col md10 s10">
          <i class="material-icons prefix">search</i>
          <input type="text" name="search" id="search" class="validate" required>
          <label for="search">Search Products</label>
        </div>
        <div class="col md2 s2">
          <input type="button" id="SubmitButtonForForm" class="btn" value="search" name="submit" value="">
        </div>
      </form>

你还必须使用JQuery进行处理:

var url = '<?php echo $_SERVER['REQUEST_URI'];?>';//Made Changes Here.
//It will output the current Page's URl for EG:http://localhost/project/shop/2  
//1 or 2 or... based on your pagination.

$("#SubmitButtonForForm").on("click",function(e){
   //Add your Code for Validations Here and put a condition that if the form is validated then only process the following code.
   var searchString = $("#search").val();
   e.PreventDefault();
   var redirectURl = url + "/" + searchString;
   //Redirect with window location method of javascript.
   window.location = redirectURl;
})