自动建议PHP代码,如谷歌搜索

时间:2012-03-20 07:06:43

标签: php search autosuggest keyword

我的产品数据库

需要强大的搜索代码

在那我需要搜索产品标题和描述中出现的关键字

当我们输入一个单词时,它应该列出单词和句子中该单词后面的下一个单词。

任何人都可以帮助使用以下代码

$sql="SELECT REPEAT (pr_name,1) AS names, REPEAT (pr_description, 1) AS pdes, REPEAT ('pr',1) AS lity FROM products WHERE pr_name like '%".$searchq."%' OR pr_description like '%".$searchq."%'".$qrtxt1." UNION SELECT REPEAT (sr_title, 1) AS names, REPEAT (sr_description, 1) AS pdes, REPEAT ('ser',1) AS lity FROM services WHERE sr_title like '%".$searchq."%' OR sr_description like '%".$searchq."%'".$qrtxt2." ORDER BY names LIMIT 10";
$result = mysql_query($sql);
echo "<ul class='livelist'>";
while($row = mysql_fetch_array($result))
  {
 $restext1='';
 $restext2='';
 $restext1= magicWords($searchq, $row['names']);
 $restext2= magicWords($searchq, $row['pdes']);

 if(!empty($restext1))
 {
    $wrappedtext1=wordwrap($restext1);
    echo "<li id=".$row['lity']." onclick='clearlist(this.innerHTML,this.id)'>".substr($wrappedtext1, 0, strpos($wrappedtext1, "\n"))."<li>";


 }
 if(!empty($restext2))
 {
    $wrappedtext2=wordwrap($restext2);
    echo "<li id=".$row['lity']." onclick='clearlist(this.innerHTML,this.id)'>".substr($wrappedtext2, 0, strpos($wrappedtext2, "\n"))."<li>";


 }

  }
echo "</ul>";

1 个答案:

答案 0 :(得分:1)

尝试本教程,您可以根据需要了解实施自动建议选项。有示例代码和演示可用。您可以使用此开发自动建议任务。 http://coding.pressbin.com/19/Implementing-Google-auto-suggest-using-jQuery

演示:http://www.pressbin.com/