KondukterCRO
KondukterCRO

Reputation: 543

Get PHP mysql array in Jquery UI autocomplete source and separate each item with comma

I have mysql database and I have table from which I need to take entire row. Than I need to put that row in jquery ui autocomplete so I can call it with my input field. Problem is that I made array with all items from row but they need to be separated in jquery. So it can't be like this:

[ {"Location":"Zagreb"}, {"Location":"Split"}, {"Location":"Zadar"}, {"Location":"Zlatar"}, {"Location":"Osijek"} ]

but it has to be like:

"Split", "Zadar", "Zlatar", "Osijek"

So my php:

$query = 'SELECT Location FROM locations';
                $result = mysqli_query ($link, $query);
                $rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;               
}



echo json_encode($rows);

And jquery autocomplete:

$( "#location1" ).autocomplete({
source: [ '<?php echo json_encode($rows);

 ?>' ]
});

Some tips?

Upvotes: 0

Views: 206

Answers (1)

whitwhoa
whitwhoa

Reputation: 2489

After your query you are appending each row array to $rows. You only want to append the value for the column Location:

$query = 'SELECT Location FROM locations';
$result = mysqli_query($link, $query);

$rows = array();
while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r['Location'];
}

Upvotes: 1

Related Questions