如何将json的输入编码为jquery ui自动完成? [关闭]

如何将json的输入编码为jquery ui自动完成?  [关闭]

问题描述:

I was already able to implement the autocomplete, it's just that what I wan to do is use the json_encode function of php.

Here's my current code:

<?php

$host = "localhost"; 
$user = "root"; 
$password = ""; 
$db = "isproj2"; 

// open connection 
$connection = mysql_connect($host, $user, $password) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 
$text = mysql_real_escape_string($_GET['term']);


$query = "Select SupplierName, SupplierID from tbl_supplier where SupplierName LIKE '%$text%'";
$result = mysql_query($query);
$json = '[';
$first = true;
while($row = mysql_fetch_array($result))
{
    if (!$first) { $json .=  ','; } else { $first = false; }
    $json .= '{"label":"'.$row['SupplierName'].'","value":"'.$row['SupplierID'].'" }';
}
$json .= ']';
echo $json;
?>

Sir/Ma'am your answers would be of great help. Thank you++

$result = mysql_query($query);
$data = array();
while ($row = mysql_fetch_array($result)) {
    $data[] = array('label' => $row['SupplierName'], 'value' => $row['SupplierID']);
}
echo json_encode($data);