2012-07-11 65 views
0

我有以下的PHP搜索腳本。
當我搜索1234567,它確切的短語匹配的,但我希望它僅匹配最初4個字符即​​1234PHP/MySQL的計數的搜索詞,並導致使用關鍵字限制

說明:

我希望它計數初始的4個字符,並顯示結果符合這些初始字符。例如。如果有人搜索1234567然後腳本應該算初始4個字符即​​1234並顯示結果。同樣,如果有人搜索456789然後腳本應該算,即最初的4個字符4567和顯示結果。

///explode search term 
      $search_exploded = explode(" ",$search); 
      foreach($search_exploded as $search_each) 
      { 
      //construct query 

      $x++; 
      if ($x==1) 
       $construct .= "keywords LIKE '%$search_each%'"; 
      else 
       $construct .= " OR keywords LIKE '%$search_each%'"; 

      } 


    //echo out construct 

    $construct = "SELECT * FROM numbers WHERE $construct"; 
    $run = mysql_query($construct); 

    $foundnum = mysql_num_rows($run); 

    if ($foundnum==0) 
     echo "No results found."; 
    { 

     echo "$foundnum results found.<p><hr size='1'>"; 

     while ($runrows = mysql_fetch_assoc($run)) 
     { 
     //get data 
     $title = $runrows['title']; 
     $desc = $runrows['description']; 
     $url = $runrows['url']; 

     echo "<b>$title</b> 
      <b>$desc</b> 
     <a href='$url'>$url</a><p>"; 
+0

什麼用腳本的問題?它沒有搜索任何錯誤? – Hardik 2012-07-11 07:28:00

+1

請不要使用新代碼'mysql_ *'功能。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-11 07:49:21

回答

1

您可以取代你的foreach

foreach($search_exploded as $search_each) 
{ 
    $str = mysql_real_escape_string(substr($search_each, 0, 4)); 
    //construct query 
    $x++; 
    if ($x==1) $construct .= "keywords LIKE '$str%'"; 
    else  $construct .= " OR keywords LIKE '$str%'"; 
} 
+0

感謝它的工作! – atif 2012-07-11 08:09:41

+0

你也可以看看這個問題嗎? http://stackoverflow.com/questions/11282461/how-to-get-multiple-row-values – atif 2012-07-11 08:10:16

0

要修改現有的代碼只能用第4個字,你只需要改變你的循環了一下:

更改這個 -

foreach($search_exploded as $search_each) { 

爲了這一點 -

for($i = 0; $i < min(count($search_exploded), 4); ++$i) { 
    $search_each = $search_exploded[$i]; 
+0

您的代碼將搜索前4個單詞。我想他想每個單詞都要搜索每4個字符。 – 2012-07-11 07:45:21

+0

@NikolaK。 - 這是可能的,但他確實說出了前4個關鍵字。在他的代碼中,他引用'關鍵字'作爲搜索字符串的元素,這些元素被分隔在空格上,所以我認爲他更有可能是指單詞。 – 2012-07-11 07:48:21

+0

你對這個問題有什麼想法嗎? http://stackoverflow.com/questions/11282461/how-to-get-multiple-row-values – atif 2012-07-11 08:14:16

0

將以下代碼添加到您的foreach循環的頂部:

if(strlen($search_each) > 4) $search_each = substr($search_each, 0, 4); 

像這樣:

$search_exploded = explode(" ", $search); 
foreach($search_exploded as $search_each) 
{ 
    if(strlen($search_each) > 4) $search_each = substr($search_each, 0, 4); 
    // your code 
} 

該代碼將搜索前4個字符中的每一個字,如果這個詞是超過4

+0

我試過了,但沒有奏效。看到這裏 - http://koolfree.com/mobile%20engine/search.php?search=0345319&submit=Search - 我已經在數據庫中的值0345319,它給出了結果,當我搜索0345319,但當我加0或1最後如03453190或03453191,它會給出Result Not Found的錯誤。 – atif 2012-07-11 07:51:20

+0

你對這個問題有什麼想法嗎? http://stackoverflow.com/questions/11282461/how-to-get-multiple-row-values – atif 2012-07-11 08:14:08