How can PHP be used to dynamically populate an HTML table with data retrieved from a MySQL database, following a specific structure like a weekly calendar?

To dynamically populate an HTML table with data retrieved from a MySQL database following a specific structure like a weekly calendar, you can use PHP to query the database for the necessary data and then loop through the results to populate the table cells accordingly. You can structure the HTML table to represent the days of the week as columns and the times of the day as rows, filling in the cells with the retrieved data.

<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to retrieve data from the database
$sql = "SELECT * FROM your_table";
$result = $conn->query($sql);

// Start building the HTML table
echo "<table>";
echo "<tr><th>Time</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th></tr>";

// Loop through the retrieved data and populate the table cells
while($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>".$row['time']."</td>";
    echo "<td>".$row['monday_data']."</td>";
    echo "<td>".$row['tuesday_data']."</td>";
    echo "<td>".$row['wednesday_data']."</td>";
    echo "<td>".$row['thursday_data']."</td>";
    echo "<td>".$row['friday_data']."</td>";
    echo "</tr>";
}

echo "</table>";

// Close the database connection
$conn->close();
?>