What are some best practices for organizing and displaying entries in a guestbook using PHP?

When organizing and displaying entries in a guestbook using PHP, it is important to sort the entries by date in descending order to show the most recent entries first. Additionally, you can limit the number of entries displayed per page to improve readability and navigation for users.

// Retrieve guestbook entries from database and sort by date in descending order
$entries = $db->query("SELECT * FROM guestbook ORDER BY entry_date DESC");

// Display a limited number of entries per page
$entriesPerPage = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $entriesPerPage;

$entries = $db->query("SELECT * FROM guestbook ORDER BY entry_date DESC LIMIT $start, $entriesPerPage");

foreach($entries as $entry) {
    echo "<p>{$entry['name']} - {$entry['message']}</p>";
}

// Pagination links
$totalEntries = $db->query("SELECT COUNT(*) FROM guestbook")->fetchColumn();
$totalPages = ceil($totalEntries / $entriesPerPage);

for($i = 1; $i <= $totalPages; $i++) {
    echo "<a href='guestbook.php?page=$i'>$i</a> ";
}