-2

Good Day! i would like to know on how to fetch data from php to html. I'm newbie with this kind of code can you help me? Here is my codes.

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

instead of this var map = L.map('map').setView([7.1309155, 125.6402975], 13); to this var map = L.map('map').setView([$row["Latitude"], $row["Longitude"]], 13);. I was hoping that you can help me with this problem.

2 Answers2

1

You use JavaScript on your code so you can not directly do what you want with JavaScript and PHP. You may use AJAX to do this. Here is a close question: How do I pass variables and data from PHP to JavaScript?

You can use this link, have a nice day.

kkrgzz
  • 452
  • 4
  • 12
1

You can echo out the array inside <script></script> like this

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

then use loop to set the map

Bevin NIno
  • 75
  • 7