What is the best way to split a large dynamically generated HTML table into multiple pages in PHP?

When dealing with a large dynamically generated HTML table in PHP, it is best to split the table into multiple pages to improve performance and user experience. One way to achieve this is by using pagination, where you display a certain number of rows per page and provide navigation links to switch between pages.

<?php

// Assuming $data is an array containing the table data
$perPage = 10; // Number of rows to display per page
$totalRows = count($data); // Total number of rows in the table
$totalPages = ceil($totalRows / $perPage); // Calculate total number of pages

$page = isset($_GET['page']) ? $_GET['page'] : 1; // Get current page number, default to 1

$start = ($page - 1) * $perPage; // Calculate starting index for the current page
$end = $start + $perPage; // Calculate ending index for the current page

echo '<table>';
for ($i = $start; $i < $end && $i < $totalRows; $i++) {
    echo '<tr>';
    foreach ($data[$i] as $value) {
        echo '<td>' . $value . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

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

?>