How can PHP developers effectively integrate pagination functionality into an online shop script?

To integrate pagination functionality into an online shop script, PHP developers can retrieve the total number of items from the database, calculate the total number of pages based on the desired number of items per page, and then adjust the SQL query to fetch only the items for the current page. Finally, display the pagination links to allow users to navigate between pages.

// Retrieve total number of items from the database
$totalItems = $db->query("SELECT COUNT(*) FROM products")->fetchColumn();

// Calculate total number of pages
$itemsPerPage = 10;
$totalPages = ceil($totalItems / $itemsPerPage);

// Adjust SQL query to fetch items for the current page
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $itemsPerPage;
$products = $db->query("SELECT * FROM products LIMIT $offset, $itemsPerPage")->fetchAll();

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