How can the PHP script be optimized to ensure that only the desired number of pages are displayed for guestbook navigation?

To optimize the PHP script for guestbook navigation, we can limit the number of pages displayed by calculating the total number of pages based on the total number of entries and the desired number of entries per page. We can then adjust the navigation links to only display the desired number of pages.

// Calculate total number of pages
$totalPages = ceil($totalEntries / $entriesPerPage);

// Display navigation links
for ($i = max(1, $currentPage - 2); $i <= min($currentPage + 2, $totalPages); $i++) {
    echo "<a href='guestbook.php?page=$i'>$i</a>";
}