douzuita7325
2012-04-08 10:43 阅读 29

如何将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++

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongyo1818 dongyo1818 2012-04-08 10:45
    $result = mysql_query($query);
    $data = array();
    while ($row = mysql_fetch_array($result)) {
        $data[] = array('label' => $row['SupplierName'], 'value' => $row['SupplierID']);
    }
    echo json_encode($data);
    
    点赞 评论 复制链接分享

相关推荐