我怎样才能将自动建议添加到我的php mysql搜索引擎?

时间:2015-07-10 06:24:26

标签: php mysql ajax

我正在尝试创建一个php mysql搜索引擎,就像电子商务搜索引擎一样,使用ajax自动建议?...我的表就像

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这样的单词时,它的工作正常我想要的是如果我开始输入一个单词'它'它会显示像

这样的自动建议
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提前....

2 个答案:

答案 0 :(得分: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("");
            }
        })
    });

答案 1 :(得分:0)

here:获取引用,这是带有ajax和auto建议的php的最佳示例

它始终适用于我。