In PHP pagination, how can the entry count be accurately calculated to avoid resetting entry numbers on each page?

When implementing pagination in PHP, the entry count should be calculated based on the total number of entries retrieved from the database query, rather than resetting the entry numbers on each page. To achieve this, you can use the SQL COUNT() function to get the total number of entries, and then use this count to determine the total number of pages needed for pagination. By doing this, you ensure that the entry numbers remain consistent across all pages.

// Assuming $query contains the SQL query to fetch entries and $resultsPerPage is the number of entries to display per page

// Get total number of entries
$countQuery = "SELECT COUNT(*) as total FROM ($query) as count";
$countResult = mysqli_query($connection, $countQuery);
$countRow = mysqli_fetch_assoc($countResult);
$totalEntries = $countRow['total'];

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