befora I ask this i tried every solution on google and here on stackoverflow.
So in my js file I have this
var locations = [
['Name','Country','City','lat','lng'],
['Name','Country','City','lat','lng'],
];
When i wrote this in file manually my mapp shown locations but I need to generate content of locations variable from mysql in php, is there something that I missing.I tried with ajax,console.log...etc
My PHP file
$result = mysqli_query($link, "SELECT hospital_name,country,city,lat,lng FROM hospitals");
$to_encode = array();
while($row = mysqli_fetch_row($result)) {
$to_encode[] = $row;
}
echo json_encode($to_encode);
I tried this but no success
$.getJSON( "process.php", function( json ) {
var locations = json;
});
-
Where's the attempted code?aldrin27– aldrin272015年09月22日 01:36:24 +00:00Commented Sep 22, 2015 at 1:36
-
2Ajax will do what you need, have a look at api.jquery.com/jquery.getjson . It gets automatically parsed to a JS arrayBen Kolya Mansley– Ben Kolya Mansley2015年09月22日 01:38:36 +00:00Commented Sep 22, 2015 at 1:38
-
var location should be defined outside of the getJson functionductiletoaster– ductiletoaster2015年09月22日 01:42:12 +00:00Commented Sep 22, 2015 at 1:42
-
I edited code still doesn't work, where do I wrong?user1594938– user15949382015年09月22日 01:42:41 +00:00Commented Sep 22, 2015 at 1:42
-
Try to console.log(json)aldrin27– aldrin272015年09月22日 01:44:58 +00:00Commented Sep 22, 2015 at 1:44
1 Answer 1
I just switched your mysqli_fetch_row to mysqli_fetch_array
$result = mysqli_query($link, "SELECT hospital_name,country,city,lat,lng FROM hospitals");
$to_encode = array();
while($row = mysqli_fetch_array($result)) {
$to_encode[] = $row;
}
echo json_encode($to_encode);
answered Sep 22, 2015 at 2:15
Marcus Abrahão
6961 gold badge8 silver badges18 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
charlietfl
Please add explanation not just code. It now becomes a game of
find the difference whereas a simple explanation of what you changed goes a long way helping others see it and understand why. Your code solution might be great....but the answer is very low quality without people being able to review it quicklydefault