【问题标题】:Change + to an & in search string在搜索字符串中将 + 更改为 &
【发布时间】:2013-12-18 06:43:32
【问题描述】:

我对 PHP 还是很陌生,并且有这个脚本用于搜索我拥有的工作数据库。问题是当查询到达这个脚本时,它看起来像这样 search-result.php?query=engineer+sydney ... 但是,我需要同时搜索这两个词,并用 & 而不是 + 出现这样的 search-result.php?query=engineer&sydney p>

这是我应该从搜索表单还是在搜索脚本本身中尝试做的事情?我在下面添加了搜索脚本和下面的表单。

任何帮助都会很棒!

<div class="joblist"> 
<?php
$query = $_GET['query']; 
$query = sanitise($query);
// gets value sent over search form

$min_length = 3;
// you can set minimum length of the query if you want

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

    $query = htmlspecialchars($query); 
    // changes characters used in html to their equivalents, for example: < to &gt;

    $query = mysql_real_escape_string($query);
    // makes sure nobody uses SQL injection

    $raw_results = mysql_query("SELECT * FROM job_jobs
        WHERE (`description` LIKE '%".$query."%') OR (`summary` LIKE '%".$query."%') OR (`title` LIKE '%".$query."%') OR (`location` LIKE '%".$query."%') ") or die(mysql_error());

    // * means that it selects all fields, you can also write: `id`, `title`, `text`
    // articles is the name of our table

    // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
    // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
    // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

        while($results = mysql_fetch_array($raw_results)){
        // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

            echo "<h3 style='padding:0;margin:0;'><a href='job-view.php?query=".$results['jid']."'>".$results['title']. "</a></h3>";
            echo "<i style='color:#999;'>Posted on: " . date("jS M Y", strtotime($results['dateposted']))."</i><br/>" . $results['summary'] . "<br/>";
            echo "Salary: " . $results['rate'] . " | Work Type: " . $results['worktype'] . " | Location: " . $results['location'];
            echo "<br/><br/>";
            // posts results gotten from database(title and text) you can also show id ($results['id'])
        }

    }
    else{ // if there is no matching rows do following
        echo "<h3>No Results</h3>Your search returned no results. Please try again.";
    }

}
else{ // if query length is less than minimum
    echo "<h3>Error</h3>The minimum length is $min_length characters. Please try again.";
}
?>
</div>

<nav class="widget-search">
                <h3>Search for a Job</h3>
                <form action="search-result.php" method="GET">
                    <button class="search-btn-widget"></button>
                    <input class="search-field" type="text" name="query" onblur="if(this.value=='')this.value='eg. Civil Engineer Perth';" onfocus="if(this.value=='eg. Civil Engineer Perth')this.value='';" value="eg. Civil Engineer Perth" />
                </form>
</nav>

【问题讨论】:

    标签: php mysql database search


    【解决方案1】:

    每当您使用 GET 方法发送数据时,它都会形成一个 NAME=VALUE 对。您看到的“+”是一些浏览器使用 % 或某些浏览器也可能使用 + 的空格。

    query=engineer+sydney 
    

    ----^名称-----^值

    $query = $_GET['query'];
    $query =str_replace(" ","&",$query);
    

    现在您可以做的是获取查询值并将空格替换为“&”或您想要的任何符号

    【讨论】:

    • 非常感谢您的帮助。
    • 很遗憾没有。我试过了,但它似乎对我不起作用。还是谢谢!
    【解决方案2】:
      $result = str_replace('+','&',$_GET["query"]);
      echo $result;
    

    谢谢

    【讨论】:

      【解决方案3】:

      用空格分隔查询变量

      $arr_query = explode(" ",$query);
      

      现在做一个动态的地方

      $where = ""
      $i=0;
      foreach($arr_query as $val)
      {
         $i+=1;
         if($i==1)
         {
               $where .= " WHERE (`description` LIKE '%".$val."%') OR (`summary` LIKE '%".$val."%') OR (`title` LIKE '%".$val."%') OR (`location` LIKE '%".$val."%') ";
         }
         else
         {
              $where .= " AND (`description` LIKE '%".$val."%') OR (`summary` LIKE '%".$val."%') OR (`title` LIKE '%".$val."%') OR (`location` LIKE '%".$val."%') ";
         } 
      }
      

      现在您可以在查询中使用此 $where,如下所示。

      $raw_results = mysql_query("SELECT * FROM job_jobs $where ") or die(mysql_error());
      

      【讨论】:

      • 谢谢。我似乎仍然无法让它工作。您能否向我展示我上面提供的整个搜索脚本的外观?对不起-我有点迷路了!
      猜你喜欢
      • 2017-12-28
      • 2017-10-24
      • 2016-11-30
      • 2011-02-22
      • 2012-02-15
      • 1970-01-01
      相关资源
      最近更新 更多