-1

I need to make an array of values fetched from a Parse Server query and call a JS function that displays markers and infoWindows on a custom Google Map.

This is not a question about server side but mostly about how to create an array and pass it to JS function in PHP.

Here's where I perform my query:

try {
    $query = new ParseQuery("Monsters");
    $mArray = $query->find();    

    for ($i = 0;  $i < count($mArray); $i++) {
        // Get Parse Object
        $mObj = $mArray[$i];

        // Get name
        $mName = $mObj->get('name');
        // Get location
        $mLocation = $mObj->get('location');
        $mLat = $mLocation->getLatitude();
        $mLng = $mLocation->getLongitude();
        // Get points
        $mPoints = $mObj->get('points');

        // Here i need to make an array of $mName + $mPoints + $mLat + $mLng and call the addMonstersOnMap() JS function...
        But my array should be like:
        [name, points, 40.7143850, -72.0059756],
        [name, points, 30.7143850, -44.0059756],
        etc..

    }

// error in query
} catch (ParseException $e){ echo $e->getMessage(); } 

And here's my JS function (I'm missing how to pass the monsters array to it and call it):

function addMonstersOnMap(monsters) { // monsters IS THE ARRAY I NEED TO GET FROM MY PHP QUERY!

    var centerCoords = new google.maps.LatLng(66.93828964, -53.64523124);

    var mapOptions = {
                zoom: 2,
                scrollwheel: true,
                center: centerCoords,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }

            var map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions);

            var marker, i;
            var infowindow = new google.maps.InfoWindow();

            // Add marker for each Monster
            for (i = 0; i < monsters.length; i++) {

                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(monsters[i][1], monsters[i][2]),
                    map: map,
                    icon: monsters[i][3]
                });


                // click function to marker, pops up infowindow
                google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infowindow.setContent(monsters[i][0]);
                        infowindow.open(map, marker);
                    }
                })(marker, i));

            }// end FOR loop

    google.maps.event.addDomListener(window, 'load', initialize);

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Frank Eno
  • 2,581
  • 2
  • 31
  • 54
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Teemu Mar 13 '18 at 16:10
  • 2
    Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) –  Mar 13 '18 at 16:12
  • none of them, it's about how to make an array and pass it to a specific JS function – Frank Eno Mar 13 '18 at 16:15
  • 2
    Then implement the question as what you exactly want to do. "_call the addMonstersOnMap() JS function_" in the middle of the PHP code says, that you want to call a JS function from PHP, which is not possible, and why is that, is clearly explained in the linked dup. – Teemu Mar 13 '18 at 16:17
  • I've read that it's possible to include a JS function in PHP: https://stackoverflow.com/a/1045885/3724800 – Frank Eno Mar 13 '18 at 16:25
  • 1
    Yes, you can include it as text, but you can't execute it. As you've read from the linked post: "_the ultimate end goal [of PHP] is ... [to] generate a string of HTML_". JavaScript in a PHP code is nothing more than text, it's just a part of that HTML string, it simply can't be executed. – Teemu Mar 13 '18 at 16:30
  • @Teemu thanks so much – Frank Eno Mar 14 '18 at 06:54

1 Answers1

1

You have 2 options.

One, send to the ajax request and the page does not recharge Your php function will return de matrix

 $.ajax({
    url: "file.php", // your file path where are the php code
    success: function(result){ // the result is the matrix
        //your code function addMonstersOnMap
    }});

or two write to js from php and update values when the page is reloaded in the code from view of the page send the matrix , and write like so that

 <?php
    echo '<script> addMonstersOnMap('.$matrix.') ;</script>';
    ?>
eborrallo
  • 750
  • 1
  • 8
  • 17