【问题标题】:Codeigniter put variable in front of the page uri segment using the pagination classCodeigniter 使用分页类将变量放在页面 uri 段的前面
【发布时间】:2017-12-21 23:22:12
【问题描述】:

我正在尝试使用 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>

请帮忙

【问题讨论】:

  • 能不能创建一个这样的URL :localhost/project/shop/2/bolts 这样就可以直接把参数作为一个Url Segment。
  • 好的。这有帮助。但是我如何将表单值提交到 URI 段
  • 您可以使用 Jquery 来实现,即在输入搜索字段后单击任何提交按钮,您可以将其重定向到上述 url。
  • 张贴您的 HTML 代码,以便有助于回答。
  • 谢谢,我已经编辑了我的问题并放入了 html 代码

标签: codeigniter pagination


【解决方案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;
})

【讨论】:

  • 谢谢。但正如我在我的问题中指定的那样,问题仅在单击下一页按钮时出现,在表单提交时,前 2 个结果很好,但是当我单击“下一页”按钮查看下一组结果时URL 被混搭了。其次,表单提交按钮不再起作用。控制台正在输出:Uncaught SyntaxError: Invalid regular expression flags。表单仅在我按 Enter 时提交。
  • 点击“下一页”生成的Url是什么?其次,我们将提交按钮更改为简单按钮,以便我们可以重定向到自定义 URL 而不是表单操作。
  • 这是提交表单时生成的 URL,它给了我准确的第一组结果:localhost/shop/shop/search?search=bolts。这是当我单击“下一页”按钮时生成的 url:localhost/shop/shop/search/bolts/2。因为搜索变量丢失并且因为 codeigniter 分页类是生成页码 2 的类,所以我的第二组结果变得不准确。
  • 所以要使所有的事情都正常工作,当使用搜索功能时,您必须在第一页中创建以下 url:localhost/shop/shop/search/bolts/1
  • 根据搜索结果,如果有很多结果,那么您的分页将使用以下链接:localhost/shop/shop/search/bolts/2
猜你喜欢
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 2014-09-23
相关资源
最近更新 更多