使用“提前”,似乎无法使用PDO获取结果。已经尽我所能用我有限的英语,找不到解决方案,也许我在PHP中做错了什么?以下是分项数字:
使用typehead.js和引导3。
HTML:
<input type="text" class="seek typeahead" id="seekInput" placeholder="Search" />联署材料:
$(document).ready(function() {
$('.seek.typeahead').typeahead({
name: "seek",
remote: 'include/search.php?store=%QUERY'
});
});Search.php以及我试图为typeahead生成数组的位置:
require_once ('konnekt.php');
$query = $db->prepare("SELECT store FROM retailers WHERE store or country LIKE :country");
$country = (isset($_POST['query']) === true) ? $_POST['query'] : '';
$query->bindValue(':country', '%' . $country . '%' );
$query->execute();
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) {
$array[] = $row;
}
echo json_encode($array);以下是我的浏览器的意思:
状况200
答复:[{"store":"Aplace"},{"store":"Aplace"},{"store":"Design Only"}]
现场发生了什么:

发布于 2013-10-29 02:38:41
typeahead.js将使用默认的数据格式呈现<p/>标记,这可以像字符串数组一样简单:
[
"Aplace",
"Aplace",
"Design Only"
]这些字符串将自动转换为基准值,这是格式的提前输入名称,它定义了数据的显示方式和查询与数据的匹配方式。
下面是如何在PHP中输出字符串数组的方法,并使用您的原始示例:
require_once ('konnekt.php');
$result = array();
$query = $db->prepare("SELECT store FROM retailers WHERE store or country LIKE :country");
$country = (isset($_POST['query']) === true) ? $_POST['query'] : '';
$query->bindValue(':country', '%' . $country . '%' );
$query->execute();
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) {
$result[] = $row["store"];
}
echo json_encode($result);如果您希望继续使用store作为数据的关键,您可以:
valueKey参数:
$('.seek.typeahead').typeahead({ name:“store”,remote:'include/search.php?store=%QUERY',valueKey:'store‘});然后还为每个数据返回所需的tokens属性:
[
{
"store": "Aplace",
"tokens": ["Aplace"]
},
{
"store": "Aplace",
"tokens": ["Aplace"]
},
{
"store": "Design Only",
"tokens": ["Design", "Only"]
}
]请注意,这些标记是单个单词的数组,它们是typeahead用于匹配您的查询的实际值。
https://stackoverflow.com/questions/19647054
复制相似问题