提前输入:返回具有多个数据的JSON数组

Typeahead: Return JSON array with multiple data

本文关键字:数据 JSON 数组 输入 返回      更新时间:2023-09-26

我正在使用bootstrap的typeahead,我想知道如何为每个结果返回具有多个数据块的JSON数组。

例如,我希望能够为每个结果返回两段数据,namedescription。如何做到这一点呢?

做到这一点的唯一方法是使用"多个数据集",如以下链接所述:http://twitter.github.io/typeahead.js/examples/多重数据

你必须创建两个不同的源:一个用于"name",一个用于"description"。

如果你不想用"description"搜索,那么你可以使用下面的代码:http://twitter.github.io/typeahead.js/examples/#custom-templates您可以使用"name"进行搜索,并且描述将显示在名称旁边。

当您键入时,此代码将使输出显示来自两列的结果。你必须改变只有在search.php代码,而不是任何其他地方。尝试一下并给出反馈。

$key=$_GET['key'];  
$mysqli = new mysqli("localhost", "root", "", "yourbd");
/* check connection */
  if ($mysqli->connect_errno) {
      printf("Connect failed: %s'n", $mysqli->connect_error);
      exit();
  }
$mysqli->set_charset("utf8");
$result = $mysqli->query("select * from yourtb where yourcolumn LIKE '%{$key}%'");
 if (!$result) {
die(mysqli_error($mysqli));
}
        $rows = array();
        while($r = mysqli_fetch_array($result)) {
             $rows[] = $r['col1']. ' - ' . $r['col2'];
        }
        echo json_encode($rows);