简体   繁体   中英

PHP Display multi-dimentional array value on google map

Can anyone help me, I am very new to PHP, maybe the answer was very obvious.

I store users details and postcodes in the database, then I need to display the postcodes as markers on the google map using javascript (this was working).

But I was trying to display each user data on google map's info window, I am only be able to display the last row of the result, so all info windows were display the same record, not the individual data to that postcode.

I think I need to combine below two queries, but I don't know how. Because the postcode need to be implode to get "postcode" to show on map.

What I am trying to achieve:

postcode need to be ("AA4 1DD", "AB1  5CD","CC4 1BBs");
infowindow (Fred, 7 street, AA4 1DD);

What I have in the Database:

name       Fred           Ann         John
address    7 street      8 road       4 line
postcode   AA4 1DD       AB1  5CD     CC4 1BB

PHP:

         try {
         $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $locations = $conn->prepare("SELECT postcode FROM tbl_data"); 
         $locations->execute();
         $lo= $locations->fetchAll(PDO::FETCH_COLUMN);
         echo  "'". $t = implode(' ,', $lo)."'";// working

        //info window
        $info_stmt = $conn->prepare("SELECT name, postcode FROM tbl_data, tbl_login WHERE tbl_data.user_id=tbl_login.user_id ");
        $info_stmt->execute();

        $result = $info_stmt -> fetchAll();

          foreach( $result as $row ) {
              echo $row[0];
           /*   echo "<pre>";
              echo gettype($row[0]);
              echo "</pre>";*/
          }

        } catch(PDOException $e) {
         echo "Error: " . $e->getMessage();
         die();
         }

Javascript:

 var addressArray = new Array(<?php echo "'". $t = implode("' ,'", $lo)."'"; ?>); //postcodes working


    var geocoder = new google.maps.Geocoder();
         for (var i = 0; i < addressArray.length; i++) {
            geocoder.geocode( { 'address': addressArray[i]}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    title: 'Click for more details'
                });

               //create info window 
            var infoWindow = new google.maps.InfoWindow({
                content: "<p>Name: </p><?php echo $row[0].'<br />'.$row[1]; ?>"
            });

Your issue is here:

 content: "<p>Food Name: </p><?php echo $row[0].'<br />'.$row[1]; ?>"

You cannot mix JavaScript and PHP like this, and $row does not - or shouldn't - actually exist in this context due to it not being within the foreach statement or - if this is a seperate JS file - you have no corresponding context.

To achieve what you need, I'd suggest using AJAX and setting a GET request to a file which then returns an array which you can then use in your JavaScript.

You can read documentation on this here .

Note, $row should be used like this: $row['ColumnName'] (not just for readability).

For example: echo $row['address'];

Example Code (PHP):

try {
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $locations = $conn->prepare("SELECT postcode FROM tbl_data"); 
     $locations->execute();
     $lo= $locations->fetchAll(PDO::FETCH_COLUMN);
     echo  "'". $t = implode(' ,', $lo)."'";// working

    //info window
    $info_stmt = $conn->prepare("SELECT name, postcode FROM tbl_data, tbl_login WHERE tbl_data.user_id=tbl_login.user_id ");
    $info_stmt->execute();

    echo $info_stmt->fetchAll();

    } catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }

Then your JavaScript:

$(document).ready(function () {
   var output[];
   $.get("phpfile.php")
     .done(function(data) {
       output = data;
     });
});

output is now the array of all your results and can be used.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM