0

I want to use gmaps.js to add map markers onto a map on my site. I'm using a PHP Script to get the latitude and longitude from my DB, however I'm using

while($dbrowinfo = mysqli_fetch_assoc($rowfields))

Therefore the information comes to an end after the Database query has been ran. Per page there will be a maximum of 10 sets of data, both with a long and lat value (therefore 20 individual variables).

Is there any way to use the variables from PHP to pass to jQuery before the script is ran? Can't really get my head around it as jQuery is client and PHP is serverside...

Thanks for your time

  • 1
    I can't figure out what you're asking. I suspect you just need the PHP script to put the values into a JS array. Then the jQuery code can loop over the JS array. You can use `json_encode()` to convert a PHP array into a JS array. – Barmar Dec 10 '15 at 20:54
  • I'm fetching $lng and $lat from my database, and want to put this into an array (I'm using MySQLi - Procedural). I don't want the rest of my results to be put into an array, just $lng and $lat, I can only find examples of PDO or OOP – PublicDisplayName Dec 10 '15 at 21:44
  • So just make an array containing only the `$lng` and `$lat` values, what's the problem? – Barmar Dec 10 '15 at 21:49

2 Answers2

1

If you are asking if there is a way to pass variables from PHP to JavaScript before the JQuery script has run, the answer is yes, definitely. All the PHP will run before any of the JavaScript, so, you can simply output the data you want as JavaScript variables, like so:

<html>

<?php
// Load the data into your PHP variables
$dataA = "some data";
$dataB = "some more data";
$dataC = 245; // numeric data
?>
<body>
    <!-- now open a script tag -->
    <script>
    var jsVar1 = "<?= $dataA ?>";// If outputting string, you need to add quotes for js
    var jsVar2 = "<?= $dataB ?>"; 
    var jsVar3 = <?= $dataC ?>; // No quotes needed for a number

    $(document).ready(function(){
        console.log("data from php: " + jsVar1 + " " + jsVar2 + " " + jsVar3);
    });
    </script>
</body>

</html>
Jason Hitchings
  • 667
  • 8
  • 10
  • +1. I'd recommend *not* using php short tags (`= $dataA ?>`) in favor of using full tags (`` unless you are confident it is php version 5.4+ (in which case short echo tags are always supported). – random_user_name Dec 10 '15 at 21:11
  • Is it possible to just use two variables (In my case $lng and $lat) and put these into an array? I have searched online however the only examples I can find is OOP or PDO and I have written the script procedurally – PublicDisplayName Dec 10 '15 at 21:48
1

Put the values in a PHP array, and then encode them into a Javascript array with json_encode

$coords = array();
while ($row = mysqli_fetchassoc($rowfields)) {
    $coords[] = array('lat' => $row['lat'], 'lng' => $row['lng']);
}
echo '<script>var coords = ' . json_encode($coords) . ';</script>';

This defines a Javascript variable coords that contains an array of objects, like:

var coords = [
    { lat: 50.0, lng: 101.1 },
    { lat: -20.8, lng: 55.3 },
    ...
];

You can then use this array in your jQuery code.

Barmar
  • 741,623
  • 53
  • 500
  • 612