What are some efficient ways to structure PHP code for displaying database content in a table format with multiple columns?

When displaying database content in a table format with multiple columns in PHP, it is important to separate the HTML markup from the PHP logic for better code organization and readability. One efficient way to structure the code is to use a loop to fetch the database records and then generate the table rows dynamically within the loop. This approach allows for easy maintenance and scalability when working with a large dataset.

<?php
// Assume $db is the database connection object

// Fetch data from the database
$query = "SELECT * FROM your_table";
$result = $db->query($query);

// Display the table headers
echo "<table>";
echo "<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>";

// Loop through the database records and display them in the table
while ($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['column1'] . "</td>";
    echo "<td>" . $row['column2'] . "</td>";
    echo "<td>" . $row['column3'] . "</td>";
    echo "</tr>";
}

echo "</table>";
?>