What are the best practices for collecting query results in an array and displaying them in a table format using PHP and HTML?

When collecting query results in an array and displaying them in a table format using PHP and HTML, it is important to properly iterate through the array and generate the table rows dynamically. This can be achieved by using a foreach loop to loop through the query results array and outputting the data within HTML table row tags.

<?php
// Assuming $queryResults is the array containing the query results

echo '<table>';
echo '<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>';

foreach ($queryResults as $result) {
    echo '<tr>';
    echo '<td>' . $result['column1'] . '</td>';
    echo '<td>' . $result['column2'] . '</td>';
    echo '<td>' . $result['column3'] . '</td>';
    echo '</tr>';
}

echo '</table>';
?>