Php 在typeahead中重复值
我在我的页面中使用typeahead,它可以显示数据库中的值,但该值将是重复的,例如,该值为hotel,它将在数据库中保存hotelhotel。有人知道解决办法吗 index.phpPhp 在typeahead中重复值,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我在我的页面中使用typeahead,它可以显示数据库中的值,但该值将是重复的,例如,该值为hotel,它将在数据库中保存hotelhotel。有人知道解决办法吗 index.php <script src="typeahead.min.js"></script> <script> $(document).ready(function(){ $('input.typeahead').typeahead({ name: 'typeahead',
<script src="typeahead.min.js"></script>
<script>
$(document).ready(function(){
$('input.typeahead').typeahead({
name: 'typeahead',
remote:'search.php?key=%QUERY',
limit : 20
});
});
</script>
<td><input type="text" name="typeahead" class="typeahead tt-query, cal_amount typeahead" autocomplete="on" spellcheck="false" value="<?php echo $res['supplier'];?>"><input type="hidden" value="" name="typeahead" /></td>
$(文档).ready(函数(){
$('input.typeahead')。typeahead({
名称:'typeahead',
远程:'search.php?键=%QUERY',
限额:20
});
});
我使用AJAX执行PHP脚本,将Typeahead字段输入作为查询参数传递,以处理SELECT以获取自动完成建议的数据。
有关更多详细信息,请访问以下链接
$(文档).ready(函数(){
$('.typeahead')。typeahead({
来源:函数(查询、结果){
$.ajax({
url:“search.php”,
数据:“查询=”+query,
数据类型:“json”,
类型:“POST”,
成功:功能(数据){
结果($.map)(数据、函数(项){
退货项目;
}));
}
});
}
});
});
search.php
<?php
$key=$_GET['key'];
$array = array();
include_once("connection.php");
$query=mysqli_query($con, "select * from supplier where cname LIKE '%{$key}%'");
while($row=mysqli_fetch_assoc($query))
{
$array[] = $row['cname'];
}
echo json_encode($array);
mysqli_close($con);
?>
<?php
$keyword = strval($_POST['query']);
$search_param = "{$keyword}%";
$conn =new mysqli('localhost', 'root', '' , 'test');
$sql = $conn->prepare("SELECT * FROM supplier WHERE cname LIKE ?");
$sql->bind_param("s",$search_param);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$countryResult[] = $row["cname"];
}
echo json_encode($countryResult);
}
$conn->close();
?>
解决方案2-无ajax。
// Create connection
$conn = new mysqli("localhost", "root", "", "test");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query=mysqli_query($conn, "select * from supplier");
$data = '';
while($row=mysqli_fetch_assoc($query))
{
// SELECT to get data for the autocomplete suggestion
$data.= '"' .$row["cname"]. '",';
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="typeahead.js"></script>
<script>
$(document).ready(function(){
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var data = [<?php echo rtrim($data, ',');?>
];
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
source: substringMatcher(data)
});
});
</script>
<input type="text" name="typeahead" class="typeahead tt-query, cal_amount typeahead" autocomplete="on" spellcheck="false" value=""><input type="hidden" value="" name="typeahead" />
//创建连接
$conn=newmysqli(“localhost”、“root”、“test”);
//检查连接
如果($conn->connect\u错误){
die(“连接失败:”.$conn->connect\U错误);
}
$query=mysqli_query($conn,“从供应商处选择*);
$data='';
while($row=mysqli\u fetch\u assoc($query))
{
//选择以获取自动完成建议的数据
$data.=''''.$row[“cname”].'',';
}
?>
$(文档).ready(函数(){
var substringMatcher=函数(strs){
返回函数findMatches(q,cb){
var匹配,substringRegex;
//将填充子字符串匹配项的数组
匹配项=[];
//用于确定字符串是否包含子字符串'q'的正则表达式`
substregex=新的RegExp(q,‘i’);
//在字符串池中迭代并查找
//包含子字符串'q',将其添加到'matches'数组中
$。每个(str,功能(i,str){
if(子相关性测试(str)){
匹配。推(str);
}
});
cb(比赛);
};
};
风险值数据=[
];
$('.typeahead')。typeahead({
提示:没错,
推荐理由:没错,
最小长度:1
},
{
来源:子字符串匹配器(数据)
});
});
请不要在一篇帖子中问多个问题。使回答、答案接受和搜索变得困难。我调整为一个问题。没有与该问题相关的代码,因为没有任何示例包含insert语句。谢谢,该代码对我有效,我正在尝试编辑代码,该代码可以在另一个td列中显示来自相同ID的其他数据,例如,该值是一家酒店的名称,另一列显示该酒店的房间价格
// Create connection
$conn = new mysqli("localhost", "root", "", "test");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query=mysqli_query($conn, "select * from supplier");
$data = '';
while($row=mysqli_fetch_assoc($query))
{
// SELECT to get data for the autocomplete suggestion
$data.= '"' .$row["cname"]. '",';
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="typeahead.js"></script>
<script>
$(document).ready(function(){
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var data = [<?php echo rtrim($data, ',');?>
];
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
source: substringMatcher(data)
});
});
</script>
<input type="text" name="typeahead" class="typeahead tt-query, cal_amount typeahead" autocomplete="on" spellcheck="false" value=""><input type="hidden" value="" name="typeahead" />