<?php
echo "<nav aria-label='Page navigation'>";
echo "<ul class='pagination justify-content-center'>";

// Calculate total pages
$total_pages = ceil($total_rows / $records_per_page);

// Range of links to show
$range = 2;

// Show links to 'first' and 'previous' page
if ($page > 1) {
    echo "<li class='page-item'><a class='page-link' href='{$page_url}&p_num=1'>First</a></li>";
    $prev_page = $page - 1;
    echo "<li class='page-item'><a class='page-link' href='{$page_url}&p_num={$prev_page}'>Previous</a></li>";
}

// Loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
    if (($x > 0) && ($x <= $total_pages)) {
        if ($x == $page) {
            echo "<li class='page-item active'><a class='page-link' href='#'>{$x}</a></li>";
        } else {
            echo "<li class='page-item'><a class='page-link' href='{$page_url}&p_num={$x}'>{$x}</a></li>";
        }
    }
}

// Show links to 'next' and 'last' page
if ($page < $total_pages) {
    $next_page = $page + 1;
    echo "<li class='page-item'><a class='page-link' href='{$page_url}&p_num={$next_page}'>Next</a></li>";
    echo "<li class='page-item'><a class='page-link' href='{$page_url}&p_num={$total_pages}'>Last</a></li>";
}

echo "</ul>";
echo "</nav>";
?>