How can PHP be used to dynamically generate and display page numbers for website pagination?
To dynamically generate and display page numbers for website pagination in PHP, you can calculate the total number of pages based on the total number of items to display and the items per page. Then, you can loop through these pages and create links to navigate to each page. This can be achieved by using a combination of PHP logic and HTML to output the pagination links.
<?php
// Get total number of items and items per page
$totalItems = 100;
$itemsPerPage = 10;
// Calculate total number of pages
$totalPages = ceil($totalItems / $itemsPerPage);
// Loop through pages and create pagination links
for ($i = 1; $i <= $totalPages; $i++) {
echo "<a href='?page=$i'>$i</a> ";
}
?>