2015-07-10 124 views
0

我试图创建一个PHP MySQL的搜索引擎的工作就像用ajax自动建议一封comerce搜索引擎吗?......我的表像如何将自动建议添加到我的php mysql搜索引擎?

id cat name 

1 men subi 
2 men flick 
3 women sheeba 
4 women leena 

我的形式是像

<html> 
<head> 
<title>search engine</title> 
</head> 
<body> 
<form action = 'ss.php' method ='GET'> 
<input type = "text" name = "q"> 
<input type = "submit" name = "submit" value = "search" 
</body> 
</html> 

我ss.php是

$k = $_GET["q"]; 
$con = mysqli_connect("localhost", "root", ""); 
mysqli_select_db($con,"x"); 
$terms=explode(" ",$k); 
$i=0; 
$set_limit = ("9"); 
$subi = ""; 
foreach ($terms as $each) 

{ 
$i++; 
$escapedSearchString = mysqli_real_escape_string($con,$each); 
if ($i == 1) 
    $subi.= " title LIKE '%$escapedSearchString%' "; 
else 
    $subi.= " AND title LIKE '%$escapedSearchString%' "; 

} 
$query = "select SQL_CALC_FOUND_ROWS * from table WHERE $subi order by  rand() limit $set_limit"; 

$qry = mysqli_query($con,"$query"); 

$row_object = mysqli_query($con,"Select Found_Rows() as rowcount"); 
$row_object = mysqli_fetch_object($row_object); 
$actual_row_count = $row_object->rowcount; 
$result = $actual_row_count; 

它的做工精细当我搜索一个词像SUBI或sheeba BT我要的是,如果我开始键入一个字的“它“会告诉像

sheeba 
subi 
sheeba in women 
subi in men 

中国汽车的建议,如果用户点击sheeba查询机器会自动变为喜欢这个

" select * from table where title like '%sheeba%' " 

,如果用户点击了“女性sheeba”的“LL查询改为喜欢这个

" select * from table where cat = 'women' and title like '%sheeba%' " 

我怎样才能得到这个? 请简要回答... TNX提前....

+0

为此使用'ajax'并显示响应的建议。 –

回答

0

如果你想创建自定义代码,而不是使用插件,那么你需要像这样

 $(document).ready(function() { 
     $("input[name='q']").on("keyup",function(event){ 
      search_value = $(this).val(); 
      // check whether the input is not empty or has characters 
      if(value.length > 0){ 
       $.ajax({ 
        url: '/path/to/file', 
        type: 'GET', 
        dataType: 'JSON', 
        data: "q="+search_value, 
        success: function(response){ 
         // create suitable html body to show your reponse 
         $("suggestion_box_id").html("your/created/htmlcontent"); 
        } 
       }) 

      } 
      else { 
       $("suggestion_box_id").html(""); 
      } 
     }) 
    }); 
+0

请提供非常简单的考试先生的答案,就像我如何更改我的表单脚本和搜索脚本?在哪里使用你的阿贾克斯编码。我无法理解你想对我说什么...... – Subi

0

here:以参考其是使用ajax和自动建议的php的最佳例子

它对我来说一直很好。