我正在尝试让Devbridge Autocomplete jQuery脚本工作,我非常接近。我可以让它给我建议(下拉值),但我需要使用它的数据属性。
建议的JSON格式如下:
{
suggestions: [
{ value: "United Arab Emirates", data: "AE" },
{ value: "United Kingdom", data: "UK" },
{ value: "United States", data: "US" }
]
}
到目前为止,我已经做到了:
{
"suggestions": [
"Show Name 1",
"Show Name 2"
],
"data": [
"1",
"2"
]
}
产生该输出的代码如下:
$reply = array();
$reply['suggestions'] = array();
$reply['data'] = array();
while ($row = $result->fetch_array(MYSQLI_ASSOC))//loop through the retrieved values
{
//Add this row to the reply
$reply['suggestions'][]=$row['SHOW_NAME'];
$reply['data'][]=$row['SHOW_ID'];
}
//format the array into json data
echo json_encode($reply);
有什么建议吗?我不知道如何将两个数据元素组合成一个数组,更不用说在它们前面加上"value"或"data"了。。。
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
$rec = array();
$rec['value'] = $row['SHOW_NAME'];
$rec['data'] = $row['SHOW_ID'];
$payload['suggestions'][] = $rec;
}
echo json_encode($payload);
$response = array();
$reply = array();
while ($row = $result->fetch_array(MYSQLI_NUM))//loop through the retrieved values
{
//Add this row to the reply
$reply['value'] = $row[0];
$reply['data'] = $row[1];
$response['suggestions'][] = $reply;
}
//format the array into json data
echo json_encode($response, JSON_PRETTY_PRINT);
不确定我是否正确,但如果你想在一个数组中获得两个值,那么使用:
$replay[][array('country' => $row['SHOW_NAME'],'data' => $row['SHOW_ID'])];