How do I encode an input to json for the jquery ui autocomplete?

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++

Answers


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

Need Your Help

Start octave with GUI from cron

linux cron octave

I would like to use a cronjob to open octave with the force-gui option.

MongoDB .Net driver 2.0 Pull (remove element)

c# mongodb mongodb-csharp

Can you help me to run correctly "Pull (remove)" with 2.0 driver.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.