I have a MySQL table like:
name | id
John | 1
Mike | 2
Kelly | 3
In Jquery, I'm able to use autocomplete showing "name" in the pop-up list while inserting the "id" in the input:
var names=[
{label:"John", value:1},
{label:"Mike", value:2},
{label:"Kelly", value:3}
];
$('#myDiv').autocomplete({
source: names
})
This is working fine, but now I need to reproduce the array of objects "names" using PHP. I'm trying the following in PHP:
$sql ="SELECT id, nome FROM table";
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
$output=array();;
foreach($rows as $key){
array_push($output, array($key{"nome"}=>intval($key{"id"})));
};
echo json_encode($output);
This is returning:
[{"John":1},{"Mike":2},{"Kelly":3}]
In Jquery, I'm now using Ajax to call myscript.php:
var names= [];
$.ajax({
url: "../myscript.php",
success: function (response) {
names = response.split(",");
}
});
But this is just displaying the same result in the cell:
[{"John":1},{"Mike":2},{"Kelly":3}]
So, I guess the soulution is to add label and value to each of the objects in PHP, or what can solve the problem?
Thanks in advance for any help!