Purtroppo ci dovrei un po' ragionare per applicarlo al tuo caso e ora non ho tempo, però ti posso indicare un buon tutorial (in inglese) che fa al caso tuo.

Da Codewalkers

La parte che t'interessa è questa:

Now, finally we can begin our navigation:

<?php
echo '&lt;div&gt;&lt;p align="center"&gt;';
echo ($pg &gt; 1) ? "$first : $prev :" : '&amp;#171; : &amp;#139; :';
?>


Above we open with a div tag, and draw the previous and next links only if we're not on the first page (otherwise we don't need them linked). Below is where we actually draw the page numbers. I've hard-coded my version to display a minimum of 5 links (if we're at the beginning or end of the total number of pages), or a maximum of 10 links (if we're somewhere in the middle of the total number of pages).

<?php
$begin = $pg - 4;
while($begin &lt; 1)
$begin++;
$end = $pg + 4;
while($end &gt; $pages)
$end--;

for($i=$begin; $i&lt;=$end; $i++)
echo ($i == $pg) ? ' ['.$i.'] ' : ' &lt;a href="'.
$_SERVER['PHP_SELF'].$query_str.$i.'"&gt;'.$i.'&lt;/a&gt; ';
?>


I'm basically just setting a $begin and $end variable ($pg + and - 4, plus the current page = 10 page links). I use the while() loops to make sure I'm not creating links to pages that don't exist. In the last line of code above, I draw links for every page in my range of page numbers, except for the current page which is instead placed inside brackets. Now we can close off the div:


Spero di esserti stato utile, ciao.