I have a while loop in this code:
while (i < 5)
{
var pos = new google.maps.LatLng(<?php echo json_encode($lat[$b]); ?>,<?php echo json_encode($lon[$b]);?>);
var marker = new MarkerWithLabel({
position: pos,
draggable: true,
raiseOnDrag: true,
map: map,
icon: 'icon.png',
labelContent: <?php echo json_encode($unidad[$b]); $b=$b+1;?>,
labelAnchor: new google.maps.Point(22, 0),
labelClass: "labels", // the CSS class for the label
labelStyle: {opacity: 0.75},
});
google.maps.event.addListener(marker, "click", function (e) { iw1.open(map, this); });
i++;
}
Now, let me explain the code and what is happening. First of all I am using JavaScript and PHP because I need some info from a db, and to add a map (Google Maps) I need to use javascript.
On the first round the value of $b
pass from 0 to 1, but going through the first loop the value resets to 0 again. It's not like $b
takes 0 by default, because if I declare $b=6
before the while loop then the values of $b
will be always 6 and 7.
What am I doing wrong? Or how should I be doing this? Any help will be appreciated. Please excuse me for any mistakes since English is not my first language.
This is my new code, i just pass the array from php to javascript (i didn't know it was so easy) here is the new code:
var lat = <?php echo json_encode($lat)?>;
var lon = <?php echo json_encode($lon)?>;
var unidad = <?php echo json_encode($unidad)?>;
while (i < <?php echo json_encode($a)?>)
{
var pos = new google.maps.LatLng(lat[i],lon[i]);
var marker = new MarkerWithLabel({
position: pos,
draggable: true,
raiseOnDrag: true,
map: map,
icon: 'icon.png',
labelContent: unidad[i],
labelAnchor: new google.maps.Point(22, 0),
labelClass: "labels", // the CSS class for the label
labelStyle: {opacity: 0.75},
});