loop through results in custom table

I am returning results from custom table. The results are an array of coordinates. I want to set map markers with each set of coordinates from the array. I need to do this dynamically, i.e.- (while there are coordinates, set a marker). However, when I try using a while loop, I get infinite loop.

each of these contains multiple values(coordinate sets): $latlong['lat'], $latlong['long']
How could I loop through each, get to the last row of table, then stop. One criteria is that the number of results will vary depending on user query.

I know that javascript for loop is used for specific number of results, but not sure how I would use that to implement this.

Ultimately, I want each result to use with this:

var places = [];

    places.push(new google.maps.LatLng( <?php echo $latlong['lat'] ?>, <?php echo $latlong['long'] ?> ) );

    for (var i = 0; i < places.length; i++) {

        var marker = new google.maps.Marker({
            position: places[i],
            map: map

    });
  • Vladislav

    Hello,

    Mixing javascript and PHP like that can easily get very confusing and hard to track. Without going too deep into the details of your data structure, perhaps it would be a bit easier to use a bit different approach - get the data from database in PHP and just output it as a JSON structure for your JS to access, for an example, like so:

    <?php
    // ... suppose $results is an array of $latlong elements, like in the OP example
    ?>
    <script>window.my_latlng_results=<?php echo json_encode($results); ?>;</script>

    Once that's in page, you can simply iterate over it in your javascript:

    var places = [];
    for (var i=0,j=my_latlng_results.length; i<j; i++) {
    	places.push(new google.maps.LatLng(
    		my_latlng_results[i]["lat"],
    		my_latlng_results[i]["long"]
    	));
    }
    // Now places is an array of google.maps.LatLng objects and it's ready to be used
    // ...

    I hope this helps, either directly or at least as a pointer. If not and you're still experiencing problems with this, can you please share the code you're trying to set up via pastebin or similar service?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.