What are some best practices for implementing pagination in PHP for grouped data?

When implementing pagination in PHP for grouped data, it is important to consider the total number of items in each group and calculate the total number of pages accordingly. You can use SQL queries to fetch the grouped data and then use PHP to paginate the results by displaying a certain number of items per page.

<?php
// Assuming $groupedData is an array of grouped data
$itemsPerPage = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $itemsPerPage;
$groupedData = array_chunk($groupedData, $itemsPerPage);

foreach ($groupedData[$page - 1] as $item) {
    // Display each item
}

// Pagination links
for ($i = 1; $i <= count($groupedData); $i++) {
    echo "<a href='?page=$i'>$i</a> ";
}
?>