2013-12-18 62 views
0

我对PHP很新颖,并且使用此脚本来搜索我的工作数据库。问题是,当查询到达这个脚本,它看起来像这样search-result.php?query=engineer+sydney ......不过,我需要寻找两个词在一起,并出现类似这样search-result.php?query=engineer&sydney&代替+将+更改为&in搜索字符串

这事我应该是试图从搜索表单或搜索脚本本身执行操作?我已经添加了下面的搜索脚本和下面的表单。

任何帮助将是伟大的!

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

回答

0

只要发送使用GET方法,它形成了一个NAME=VALUE pair.and的“+”你看到的是空白的一些浏览器使用%或一些可以使用+也数据。

query=engineer+sydney 

---- ^名字------- ^值

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

你可以做什么是“&”或任何迹象读取查询值,更换空白你想

+0

非常感谢您的帮助。 – verrucktfuchs

+0

@verrucktfuchs是否解决了您的问题? –

+0

不幸的不是。我尝试过,但它似乎没有为我工作。不管怎么说,还是要谢谢你! – verrucktfuchs

-1

打破你的查询变量与空间

$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."%') "; 
    } 
} 

现在你可以使用这个$在你在哪里查询如下。

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

感谢您的支持。我似乎仍然无法使其工作。你能告诉我整个搜索脚本与我上面提供的那个一样吗?对不起,我只是有点失落! – verrucktfuchs

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

感谢