gpt4 book ai didi

php - 如何向我的 php mysql 搜索引擎添加自动建议?

转载 作者:行者123 更新时间:2023-11-29 22:14:26 26 4
gpt4 key购买 nike

我正在尝试创建一个像电子商务搜索引擎一样工作的 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 这样的单词时,它工作得很好,但我想要的是,如果我开始输入单词 's',它会显示自动建议,例如

sheeba
subi
sheeba in women
subi in men

如果用户点击 sheeba,查询将自动更改为这样

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

如果用户点击“sheeba in Women”,“查询将更改为这样”

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

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

最佳答案

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

     $(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("");
}
})
});

关于php - 如何向我的 php mysql 搜索引擎添加自动建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31333851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com