将 + 更改为搜索字符串中的 &


Change + to an & in search string

我是PHP的新手,我正在使用这个脚本来搜索我的数据库中的工作。问题是当查询到达此脚本时,它看起来像这样search-result.php?query=engineer+sydney......但是,我需要同时搜索这两个单词,并且看起来像这样search-result.php?query=engineer&sydney,带有&而不是+

这是我应该尝试从搜索表单还是在搜索脚本本身中执行的操作?我在下面添加了搜索脚本,并在下面添加了表单。

任何帮助都会很棒!

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

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

query=engineer+sydney 

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

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

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

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

谢谢

用空格断开查询变量

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