<div class='navil'><?
if ($page != 1) {$pnaz = '<table width=100% style=margin-bottom: 5px;>
<tr>
<td>
<span class=textgray><a href= '.$domen.'/'.$upag.'n='. ($page - 1) .'>&larr; Назад</a><span></td>
<td width=82% align=center>';

}else{$pnaz = '<table width=100% style=margin-bottom: 5px;>
<tr>
<td>
<span class=textgray>&larr; Назад</span></td><td width=82% align=center>';}

if ($page != $obpag){$pdal = '</td><td>
<span class=textgray><a href= '.$domen.'/'.$upag.'n='. ($page + 1) .'>Вперёд &rarr;</a></span></td></tr></table>';

}else{$pdal = '</td>
<td>
<span class=textgray>Вперёд &rarr;</span></td></tr></table>';}

if($obpag <= 11){
  for($i = 1,$ip = 0; $i < $page; $i++,$ip++){
      if($page - $i > 0) $pleft .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='. $i .'>'. $i .'</a>';
    }

    for($i = 1; $i < 12 - ($ip + 1); $i++){
      if($page + $i <= $obpag) $pright .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='. ($page + $i) .'>'. ($page + $i) .'</a>';
    }
}else{
if($page == 1){
    for($i = 1; $i < 9; $i++){
      if($page + $i <= $obpag) $pright .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='. ($page + $i) .'>'. ($page + $i) .'</a>';
    }

    $pright .= '&nbsp;...&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='.$obpag.'>'.$obpag.'</a>';
}elseif($page > 1 and $page < 7 ){
    for($i = 1,$ip = 0; $i < $page; $i++,$ip++){
      if($page - $i > 0) $pleft .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='. $i .'>'. $i .'</a>';
    }

    for($i = 1; $i < 10 - ($ip + 1); $i++){
      if($page + $i <= $obpag) $pright .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='. ($page + $i) .'>'. ($page + $i) .'</a>';
    }

    $pright .= '&nbsp;...&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='.$obpag.'>'.$obpag.'</a>';
}elseif($page >= 7 and $page+7 <= $obpag){
    $pleft .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n=1>1</a>&nbsp;...';

    for($i = 1,$ip = 0; $i < 4; $i++,$ip++){
      if($page - $i > 0) $pleft .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='. ($page - 4 + $i) .'>'. ($page - 4 + $i) .'</a>';
    }

    for($i = 1; $i < 4; $i++){
      if($page + $i <= $obpag) $pright .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='. ($page + $i) .'>'. ($page + $i) .'</a>';
    }

    $pright .= '&nbsp;...&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n='.$obpag.'>'.$obpag.'</a>';
}elseif( $page+6 >= $obpag and $page != $obpag and $page != 1){
    $pleft .= '&nbsp;<a class=selpage href= '.$domen.'/'.$upag.'n=1>1</a></span>&nbsp;...';

    for($i = 1; $i < $page; $i++){
      if($page - $i > 0) {$ip++;}
    }

    for($i = 1,$vop=1; $i < $obpag - $page + 1; $i++){
      if($page + $i <= $obpag) {$vop++;$pright .= '&nbsp;<a class=selpage href= '.$domen.'/index.php?'.$upag.'&amp;n='. ($page + $i) .'>'. ($page + $i) .'</a>';}
    }

    for($i = 1, $ops = 1; $i < 10 - $vop; $i++){
      if($page - $i > 0) {$ops++;}
    }

    for($i = 1; $i < 10-$vop; $i++){
      if($page - $i > 0) {$pleft .= '&nbsp;<a class=selpage href= '.$domen.'/index.php?'.$upag.'&amp;n='. ($page - $ops + $i) .'>'. ($page - $ops + $i) .'</a>';}
    }
}elseif($page == $obpag){
    $pleft .= '&nbsp;<a class=selpage href= '.$domen.'/index.php?'.$upag.'&amp;n=1>1</a>&nbsp;...';

    for($i = 1; $i < 9; $i++){
      if($page - $i > 0) $pleft .= '&nbsp;<a class=selpage href= '.$domen.'/index.php?'.$upag.'&amp;n='. ($page - 9 + $i) .'>'. ($page - 9 + $i) .'</a>';
    }
}
}

echo $pnaz.$pleft.'&nbsp;<span class=selpage-act>'.$page.'</span>'.$pright.$pdal;
?></div>