Try to show as materials inputs the number of pages but encountered a problem. If I have many pages they all appear to me. (Page1 pag2 pag4 pag5 page 3 and so on until the last)

I'd like to do something like Back pag1 pag2 pag3 pag4... last page Next

Code:
<?php
if ($page+1>1)
{
    echo "<a class='letter_box' style='margin-bottom:2px; margin-top:2px;' href='/muzica-online/" . $page . ".html'>Inapoi</a>";
}
for ($j=1; $j<=$nr_pag; $j++)
{
    echo "
    <a class='letter_box' style='margin-bottom:2px; margin-top:2px;' href='/muzica-online/".$j.".html'>".$j."</a>";    
}
if ($page+1<$nr_pag)
{
    echo "&nbsp;<a class='letter_box' style='margin-bottom:2px; margin-top:2px;' href='/muzica-online/" . ($page + 2) . ".html'>Inainte</a>";
}
?>