首页 > 解决方案 > 如何从php获取数据到html

问题描述

再会!我想知道如何从 php 获取数据到 html。我是这种代码的新手,你能帮我吗?这是我的代码。

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
    <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
    <style>
        #map {position: absolute; top: 0; bottom: 0; left: 0; right: 0;}
    </style>
</head>
<body>
    <?php
$mysql_hostname = "localhost";
$mysql_username = "root";
$mysql_password = "root";
$mysql_database = "dtable";
$conn= mysqli_connect($mysql_hostname,$mysql_username,$mysql_password,$mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, Longitude, Latitude FROM androidtable where Emp_ID = '212'  order by Date_log desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["Longitude"] . "</td><td>"
. $row["Latitude"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
    <div id = "map"></div>

    <script>
        
    var map = L.map('map').setView([7.1309155, 125.6402975], 13);
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

    L.marker([7.1309155, 125.6402975]).addTo(map)
    .bindPopup('Employee 212 Location.<br> RealTime Tracker.')
    .openPopup();
    </script>
</body>
</html>

而不是这个var map = L.map('map').setView([7.1309155, 125.6402975], 13);到这个var map = L.map('map').setView([$row["Latitude"], $row["Longitude"]], 13);。我希望你能帮助我解决这个问题。

标签: phphtml

解决方案


您在代码中使用 JavaScript,因此您无法直接使用 JavaScript 和 PHP 做您想做的事情。您可以使用 AJAX 来执行此操作。这是一个很接近的问题:如何将变量和数据从 PHP 传递到 JavaScript?

您可以使用此链接,祝您有美好的一天。


推荐阅读