2013-12-18 101 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; 

感謝