Skip to content
PHP Logo

How can the PHP code be modified to display multiple bookings for the same room in a single row in the table?

To display multiple bookings for the same room in a single row in the table, we can modify the PHP code to group the bookings by room and then display them together in a single row. This can be achieved by using a SQL query to fetch all bookings for each room and then looping through the results to display them in a single row in the table.

php
<?php
// Assuming $mysqli is the MySQLi connection object

// Fetch all bookings grouped by room
$sql = "SELECT room_id, GROUP_CONCAT(booking_date) AS booking_dates FROM bookings GROUP BY room_id";
$result = $mysqli->query($sql);

// Display the bookings in a table
echo "<table>";
echo "<tr><th>Room ID</th><th>Booking Dates</th></tr>";
while($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['room_id'] . "</td>";
    echo "<td>" . $row['booking_dates'] . "</td>";
    echo "</tr>";
}
echo "</table>";

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