How can the code snippet be improved to prevent the issue of displaying an empty table when reaching the last page?

The issue of displaying an empty table when reaching the last page can be solved by checking if there are any records to display before rendering the table. This can be achieved by adding a condition to check if the number of records retrieved from the database is greater than 0 before displaying the table.

<?php
// Assuming $records is the array of records retrieved from the database
if(count($records) > 0) {
    echo "<table>";
    // Display table headers
    echo "<tr><th>Column 1</th><th>Column 2</th></tr>";
    
    // Display table rows
    foreach($records as $record) {
        echo "<tr><td>".$record['column1']."</td><td>".$record['column2']."</td></tr>";
    }
    
    echo "</table>";
} else {
    echo "No records found.";
}
?>