我正在为我的搜索框使用自动完成用户界面。下面是我的 php 代码:
<?php
include 'connect.php';
if (isset($_GET['term'])) {
$value = $_GET['term'] . '%';
$return_arr = array();
$stmt = $conn->prepare("SELECT * FROM jobs WHERE jobname LIKE ? or formtype LIKE ?");
$stmt->bind_param("ss", $value, $value);
$stmt->execute();
$stmt->bind_result($entryid, $jobnumber, $jobname, $formtype, $date);
while ($stmt->fetch()) {
$return_arr[] = $jobname;
$return_arr[] = $formtype;
}
echo json_encode($return_arr);
}
?>
一切都运行得很好。但我有点希望 while 语句在 $formtype
值之前首先返回所有 $jobname
值。简而言之,我希望按列而不是按行返回值。我不确定这怎么可能,因为我尝试将它们放入 do while 和 foreach 语句中,但两者都不适合我。
另外由于某种原因,当我创建另一个 echo 语句时,:
echo json_encode($return_arr);
停止工作。
最佳答案
. But I kind of want for the while statement to return all $jobname values first before the $formtype values.
构建两个数组,然后合并它们:
$ar1 = [];
$ar2 = [];
while($stmt->fetch()) {
$arr1[] = $jobname;
$arr2[] = $formtype;
}
$return_arr = array_merge($arr1, $arr2);
Also for some reason, when I create another echo statement, the: echo json_encode($return_arr); stops working.
因为自动完成需要 json 对象,而你想尝试给他 json 对象和其他东西
关于javascript - 自动完成 json_encode 按列返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37969356/