How can the PHP code be optimized to display multiple reservations for the same customer in a single table row?
To display multiple reservations for the same customer in a single table row, we can group the reservations by customer ID and then iterate through each group to display the reservations in a single row. This can be achieved by using a SQL query to fetch the reservations grouped by customer ID and then iterating through the results to display them in a table row.
<?php
// Assuming $pdo is the PDO object connected to the database
// Fetch reservations grouped by customer ID
$query = "SELECT customer_id, GROUP_CONCAT(reservation_id SEPARATOR ', ') AS reservations FROM reservations GROUP BY customer_id";
$stmt = $pdo->query($query);
// Display reservations in a table row
echo "<table>";
echo "<tr><th>Customer ID</th><th>Reservations</th></tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>{$row['customer_id']}</td>";
echo "<td>{$row['reservations']}</td>";
echo "</tr>";
}
echo "</table>";
?>
Related Questions
- How can PHP code be optimized for better performance and readability when working with form submissions and file manipulation?
- Is it considered good practice to dynamically increment array names in PHP based on row count?
- What measures can be taken to prevent emails from being filtered by spam filters, such as Norton, due to missing or incorrect header parameters in PHP?