2012-07-11 42 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