1

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)
            },
        }
    ]
6
  • 1
    Looks like an issue with the illegal string literal termination Commented May 5, 2015 at 10:31
  • 1
    not sure about the PHP syntax lat.push(<?php echo '\'$lat\''; ?>); Commented May 5, 2015 at 10:32
  • 1
    You cannot have a for loop statement inside a literal array statement Commented May 5, 2015 at 10:36
  • @PatrickEvans the for loops is outside of the while loop, is that what you mean? Commented May 5, 2015 at 10:52
  • 1
    No I mean, markers: [ for (var .... you would get an error trying to do this. Commented May 5, 2015 at 16:30

2 Answers 2

3

You could make the work on the PHP side :

<?php 
    $points = array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $point = new stdClass();
        $coords = array();
        $coords[] = floatval($row['latitude']);
        $coords[] = floatval($row['longitude']);
        $point->latLng = $coords;
        $point->name = $row['site_name'];

        array_push($points, $point);
    }
?>

<script>
var points = <?php echo json_encode($points); ?>;
</script>

And then init your maps with the javascript variable :

markers: points
Sign up to request clarification or add additional context in comments.

2 Comments

I'm having a play around to try and get it to work, seems like a few errors kick up. I'll sort it and check to see if we have any luck
Just changed the var points to an echo, $points to an array push and it works perfectly. Thank you
1

You already json_encode() the values, hence the single quotes in js are breaking it. Use:

site.push(<?php  echo $site; ?>);

1 Comment

ah yes, this is true thank you for pointing that out. However, It apparently isn't the only school-boy-error in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.