1

I am currently working on a website that displays markers on Google Maps based on PHP data from a mySQL database. I recently figured out how to connect the data together, but the loop that is supposed to print multiple markers only prints out one. I browsed this website for similar questions such as this but either found out of date code or code that I could not understand.

Goal: Display multiple different markers on the maps Problem: Even though the loop is supposed to print 4 of the markers, it only prints one.

Code:

 var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 2,
            center: {lat: 25.363, lng: 131.044}
 });

 var marker, i;
 var count = <?php echo $count;?>;
 for (var i = 0; i < count; i++) {
    marker = new google.maps.Marker({
              position: {
                        lat: <?php echo $lat[$count2]; ?>,
                        lng: <?php echo $long[$count2]; ?>
               },
               map: map,
    });

 }
apaul
  • 308
  • 3
  • 13
  • PHP and javascript can not be used interactively like that ... php preprocesses code, and sends html to the browser, at which point PHP is finished with the page – Jaromanda X Mar 17 '19 at 06:52
  • besides, your code clearly only ever accesses a single value ... `$lat[$count2];` so of course it'll always be the same, even if PHP and javascript had some sort of communication channel between them (which they don't) – Jaromanda X Mar 17 '19 at 06:52
  • There's nothing "out of date" about that code in the link - it's just that the code in that link isn't even using PHP, so does not fall into the trap of thinking PHP and javascript are interactive – Jaromanda X Mar 17 '19 at 06:55
  • @JaromandaX I appreciate your response. I had $count2++ in my code but I forgot to add it again after simplifying it a bit for this question. For your first statement, I am a bit confused because I am able to access the data like this from PHP. I referred to this [source](https://www.dyn-web.com/tutorials/php-js/scalar.php). – apaul Mar 17 '19 at 06:55
  • again, that would require a two way communication that does not exist - a javascript for loop will not cause a php variable to be incremented - your link shows that no such two way communication exists – Jaromanda X Mar 17 '19 at 07:00
  • @JaromandaX Thank you for letting me know. I definitely misunderstood it. What method can I use it to increment the count and make it interactive? – apaul Mar 17 '19 at 07:04
  • you can't, no such interaction exists - try sending the whole "array" of results in a variable ... as I can't see your php, I can't suggest HOW to do it, but it would be along the lines of `var lat = ;` and similar for `$long` - then the lat's/long's will be available to javacxript – Jaromanda X Mar 17 '19 at 07:12
  • see https://stackoverflow.com/a/15128862/5053002 for example that should clarify things for you – Jaromanda X Mar 17 '19 at 07:17

0 Answers0