Okay. So i've been here all day, reading and following all the solutions about this topic but i got nothing. Nothing works. Can you show me how to do it, with complete and understandable example. Im new to JavaScript and PHP, so how can i get array values from a PHP file to JavaScript. I tried doing this: var locations = <?php echo json_encode($location); ?>;
but i gives me error. and no one answered why. This code here:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [
['Municipal Hall', 6.414333734068895, 125.61093270778656, 1],
['Camarillo Household', 6.4345278, 125.58975, 2],
['Perez Household', 6.4343889, 125.59202777777777, 3],
['Usman Household', 6.4338056, 125.59191666666666, 4],
['Lim Household', 6.4333889, 125.59419444444444, 5]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(6.40, 125.60),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</body>
</html>
i want to change the value of variable locations
from the value from my database. so i got this PHP file:
<?php
$servername = "host";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql='SELECT a.Household_Name, b.Latitude, b.Longitude FROM household a, location b WHERE a.Household_ID = b.Household_ID;';
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
// output data of each row
for($i=0;$i<mysqli_num_rows ( $result );$i++){
$row=mysqli_fetch_row($result);
$location[]= array($row[0].', '.$row[1].', '.$row[2].','.($i+1));
//echo "Household Name: " . $row[0]. " - Latitude: " . $row[1]. " - Longitude " . $row[2]. " " .($i+1)."<br>";
}
}else{echo "0 results";}
?>
it works fine. it outputs the data from the database. now what i want to know is how can i convert that value from the database to a value i can use in place of the locations
variable so that the markers will appear on the map? var locations = <?php echo json_encode($location); ?>;
this guy give me error, i followed every instruction i can, but still it's error. can u modify my code so that it works or can u just point me out in the WORKING/FUNCTIONAL code there is to your knowledge. Please, help me. I'm in deep trouble here.