This map.php page displays a table containing mongoDB documents which contain a time, latitude and longitude for coordinate readings.
It also uses the google API to show a location on a map (hardwired). I want to replace the lat/long being read by the google map script with the latitude/longitude of the table entry with the largest 'Time' value (aka the latest entry). I understand that you can write php echo statements straight into to get the values but as you can see I have a loop to get the table entries, is it realistic to do what I want here? PHP learner, absolute mongodb and javascript novice here. Thanks for any help!
EDIT: SOLVED, edited to working solution as per selected answer. thanks all.
<!DOCTYPE html>
<html>
<head>
<style>
/* Set the size of the div element that contains the map */
#map {
height: 400px; /* The height is 400 pixels */
width: 100%; /* The width is the width of the web page */
}
</style>
</head>
<body>
<h3>Last Locations</h3>
<?php
//name of DB: muntean Name of collection:TestData
$user = "muntean";
require '/var/composer/vendor/autoload.php';
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$collection = new MongoDB\Collection($manager, $user, 'testData');
$data = "<table style='border:1px solid red;";
$data .= "border-collapse:collapse' border='1px'>";
$data .= "<thead>";
$data .= "<tr>";
$data .= "<th>Time</th>";
$data .= "<th>Longitude</th>";
$data .= "<th>Latitude</th>";
$data .= "</tr>";
$data .= "</thead>";
$data .= "<tbody>";
try{
$cursor = $collection->find();
$largest = 0;
foreach($cursor as $document){
if ($largest < $document["Time"]) {
$largest = $document["Time"];
$longitude = $document["Longitude"];
$latitude = $document["Latitude"];
}
$data .= "<tr>";
$data .= "<td>" . $document["Time"] . "</td>";
$data .= "<td>" . $document["Longitude"]."</td>";
$data .= "<td>" . $document["Latitude"]."</td>";
$data .= "</tr>";
}
$data .= "</tbody>";
$data .= "</table>";
echo $data;
}catch(MongoException $mongoException){
print $mongoException;
exit;
}
?>
<!--The div element for the map -->
<div id="map"></div>
<script>
// Initialize and add the map
function initMap() {
// The location of point
var point = {lat: <?php echo $latitude; ?>, lng: <?php echo $longitude; ?>}
// The map, centered at point
var map = new google.maps.Map(
document.getElementById('map'), {zoom: 12, center: point});
// The marker, positioned at point
var marker = new google.maps.Marker({position: point, map: map});
}
</script>
<!--Load the API from the specified URL
* The async attribute allows the browser to render the page while the API loads
* The key parameter will contain your own API key (which is not needed for this tutorial)
* The callback parameter executes the initMap() function
-->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD6CMFIF-m8Z_kNLUGT7HEVBew_wPLno7o&callback=initMap">
</script>
</body>
</html>

var my_var = <?php echo json_encode($my_var); ?>;