Are there any best practices or design patterns for handling pagination in PHP applications?

When handling pagination in PHP applications, it's important to implement a consistent and user-friendly way to navigate through large sets of data. One common approach is to use a combination of SQL queries to fetch a subset of data and display it on multiple pages. Additionally, using URL parameters or session variables can help keep track of the current page and the number of items per page.

// Assuming $conn is the database connection object

// Define the number of items per page
$itemsPerPage = 10;

// Get the current page number from the URL parameter
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// Calculate the offset for the SQL query
$offset = ($page - 1) * $itemsPerPage;

// Fetch data from the database using LIMIT and OFFSET
$stmt = $conn->prepare("SELECT * FROM your_table LIMIT :limit OFFSET :offset");
$stmt->bindValue(':limit', $itemsPerPage, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();

// Display the data on the page
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Display your data here
}

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