My for-each not working. Not looping javascript code. Could someone please provide some code to help?
$guy= queryMysql("SELECT lat, long FROM members WHERE user='$guy'"); while($data2 = mysql_fetch_array($guy)){ $latitude1= $data2['lat']; $longitude1= $data2['long']; echo "<script>function createMarker() { $.goMap.createMarker( { latitude: $latitude1, longitude: $longitude1, animation: google.maps.Animation.DROP, title: 'Current users location', html: { content: '<p>This is your location $friend</p>', popup: false } } ); }</script>";
You'd need to build an array in your PHP loop, and provide that data to your javascript. Something like this could work:
PHP
<?php
$strOut = '';
if (sizeof($following)) {
foreach ($following as $friend) {
$friendsloc = queryMysql("SELECT homelocation, currentlocation FROM members WHERE user='$friend'");
while ($data2 = mysql_fetch_array($friendsloc)) {
$latitude1 = $data2['homelocation'];
$longitude1 = $data2['currentlocation'];
$strOut .= '{"lat": '.$latitude1.', "lon": '.$longitude1.'},';
}
}
}
$strOut = 'var locations = [' . rtrim($strOut,",") . ']';
?>
JavaScript:
$(document).ready(function() {
// get a Google map centred roughly on the John Dalton Building:
$('#map').goMap({
latitude: 53.472342399999995,
longitude: -2.2398096,
zoom: 12,
maptype: 'ROADMAP',
scaleControl: true
});
<?php echo $strOut; ?>
// now add a marker:
for(var i = 0; i < locations.length; i++) {
$.goMap.createMarker({
latitude: locations[i].lat,
longitude: locations[i].lon,
animation: google.maps.Animation.DROP,
title: 'Current users location',
html: {
content: '<p>This is your location </p>',
popup: false
}
});
}
});
You create numerous createMarker() functions and the last overrides all previous ones. So when you call it later, you get exactly one call with the last parameters. Do not create a function, push the coordinates to some storage and loop over it afterwards.
<?php
$friendsloc = mysql_query("SELECT lat, long FROM members WHERE user='$friend'");
while($data2 = mysql_fetch_array($friendsloc)){
$latitude1= $data2['lat'];
$longitude1= $data2['long'];
echo "<script>function createMarker() {
$.goMap.createMarker(
{
latitude: $latitude1,
longitude: $longitude1,
animation: google.maps.Animation.DROP,
title: 'Current users location',
html: {
content: '<p>This is your location $friend</p>',
popup: false
}
}
);
}</script>";
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.