0

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?

1 Answer 1

1

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'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes that's it. Thank you very much. And one thing for someone else who needs help. Remove [ '' ] from source because it's allready in php variable rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.