You can allways have a look at
isset()
If you like to count number of rows you can use
$result = mysql_query("SELECT * FROM ****"); // the query
$foundamount = mysql_num_rows($result); // the number of rows returned
In your case you look to do
$numpages = ceil($foundamount / $pagernum) // finding number of pages
// then put this inside a loop
for ($i=1; $i < $numpages+1; $i++) {
if($i < $pagenum+20 and $i > $pagenum-1 ){
// code
}
Hope this helps some...
regs