gpt4 book ai didi

javascript - 使用 PHP 值填充 JavaScript 变量

转载 作者:行者123 更新时间:2023-11-30 09:16:06 25 4
gpt4 key购买 nike

这个 map.php 页面显示了一个包含 mongoDB 文档的表格,其中包含坐标读数的时间、纬度和经度。

它还使用 google API 在 map 上显示位置(硬连线)。我想用具有最大“时间”值(又名最新条目)的表条目的纬度/经度替换谷歌地图脚本读取的纬度/经度。我知道您可以直接编写 php echo 语句来获取值,但是正如您所看到的,我有一个循环来获取表条目,在这里做我想做的事情现实吗? PHP学习者,绝对的mongodb和javascript新手看这里。感谢您的帮助!

编辑:已解决,根据所选答案编辑为工作解决方案。谢谢大家。

   <!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>

位置随机What it looks like, map should be centered on the table datta though

最佳答案

您只需在 PHP 中将您的变量编码为 Json 即可使用 JS 获取字符串。然后你解码 Json,你将在一个漂亮的 JS 对象中拥有你的变量

<script>
let json= "<?php echo json_encode($variables); ?>";
let obj = JSON.parse(json);
</script>

关于javascript - 使用 PHP 值填充 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55019162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com