Question
I'm trying to place markers on a map by retrieving them via php, looping them into javascript arrays then looping the arrys to add the markers.
db query
require_once("func/connect.php");
$query = "SELECT * FROM site_locations;";
$stmt = $db->prepare($query);
$stmt->execute();
creating creating js arrays
<script type="text/javascript">
var lat = new Array();
var lon = new Array();
var site = new Array();
<?php
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$lat = json_encode($row['latitude']);
$long = json_encode($row['longitude']);
$site = json_encode($row['site_name']);
?>
lat.push(<?php echo '\'';
echo $lat;
echo '\''; ?>);
lon.push(<?php echo '\'';
echo $lon;
echo '\''; ?>);
site.push(<?php echo '\'';
echo $site;
echo '\''; ?>);
<?php
}
?>
</script>
Finally adding looping the js arrays to add markers
markers: [
for (i = 0; i < lat.length; i++) {
{
latLng: [lat[i], lon[i]],
name: site[i]
},
}
]
As it currently stands, this does not work. The PHP side of things works.
Also tried
markers: [
for (var i = 0; i < lat.length; i++) {
{
latLng: [lat(i), lon(i)],
name: site(i)
},
}
]
lat.push(<?php echo '\'$lat\''; ?>);markers: [ for (var ....you would get an error trying to do this.