php - Codeigniter:不使用 $_GET 实现搜索过滤器

标签 php search codeigniter codeigniter-url

我有一个应用程序,其中有一个名为项目的表,我希望能够对其进行搜索。我更愿意使用现有的方法来调用模型来获取查询结果。所以我现在的 URL 格式是 example.com/projects/id/slug,所有项目都是 example.com/projects。我想要一个搜索表单,将关键字作为字符串传递给该方法。

我知道 CI 默认不允许 $_GET:

来自 CodeIgniter 关于安全性的手册:

GET, POST, and COOKIE Data

GET data is simply disallowed by CodeIgniter since the system utilizes URI segments rather than traditional URL query strings (unless you have the query string option enabled in your config file). The global GET array is unset by the Input class during system initialization.

我的问题是如何以这种方式将 URI 段与多个关键字一起使用?

我可以做类似搜索/关键字+第二个关键字+第三个关键字的操作吗?

使用表单是否可以将文本框中的关键字转换为上述格式?

谢谢

比利

最佳答案

如果你想这样做......

您可以这样做,假设有多个帖子输入并且它们仅用于搜索,它可能看起来像这样

function search(){

  $url = $this->uri->segment(2);

  if(empty($url)){
      if((isset($_POST) && (!empty($_POST))
      {
         $search_seg = '';

         foreach($_POST as $k => $v){
             // I always make sure to use the CI post w/ 
             // TRUE set in the second param so as to XSS filter the user input
             $var = $this->input->post($k, TRUE);
             // Just incase the user input had a plus in it?
             $var = str_replace('+', '%20', $var)
             // Concatenate the search string
             $search_seg .= $var . '+';
         }

         // Make the url, use substr() to strip the last + off the end 
         $search_seg = 'search/' . substr($search_seg, 0, -1);

         /* Make sure CI's URL helper is enabled
            $this->load->helper('url'); if it hasn't been loaded
            This will give the illusion that the form post went to this URL,
            thus doing what you're looking for... */
         redirect($search_seg, 'location');

      }else{
         // There was no post, do whatever
      }
  }else{
      $search_arr = explode('+', $url);
  }

尽管有一些方法可以重新创建 $_GET 数组并仍然将它们与 CI 样式 URL 一起使用,但上面的内容应该与您所描述的完全一致,尽管这有点复杂

其他信息:

如果只有一个搜索输入字段,并且,例如,术语由空格分隔,那么您可能想要这样做(可能需要一些正则表达式过滤)...替换 foreach($_POST as $k => $v)... 循环如下:

$keywordinput = $this->input->post('keywords', TRUE);
$keywordinput = trim($keywordinput);

$keywords = explode(' ', $keywordinput);

foreach($keywords as $word){
    if(!empty($word)){
       $word = str_replace('+', '%20', $var)
       $search_seg .= $word . '+';
    }
}

关于php - Codeigniter:不使用 $_GET 实现搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4783110/

相关文章:

php - 自动重命名多个文件一次 - ddmmyyyy.file 至 yyyymmdd.file

php - 多个 memcached 服务器问题

elasticsearch - Elasticsearch对包含空格的短语进行匹配

php - 在 Git 中维护 "version for the server"的正确方法是什么 - 仅更改配置文件?

php - 将 mysql 数据转换为 json 对象

php - Codeigniter 表单验证安全性

php - 设置和配置 MySQL 数据库以存储有关十亿个唯一 URL 的信息

php - 允许用户检查嵌入式YouTube视频中的起点和终点

c# - 在 C# 中为信息检索应用程序编写倒排索引

ios - 使用 NSPredicate 搜索/过滤自定义类数组