<?
if ($page != 1) {$pnaz = '&nbsp;<span class="navi"><a href= '.$domen.'/'.$upag.'n='. ($page - 1) .'>Назад</a></span>';
}else{$pnaz = '&nbsp;<span class="vnavi">Назад</span>';}

if ($page != $obpag){$pdal = '&nbsp;<span class="navi"><a href= '.$domen.'/'.$upag.'n='. ($page + 1) .'>Далее</a></span>';
}else{$pdal = '&nbsp;<span class="vnavi">Далее</span>';}

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

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

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

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

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

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

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

    $pright .= '&nbsp;...&nbsp;<span class="navi"><a href= '.$domen.'/'.$upag.'n='.$obpag.'>'.$obpag.'</a></span>';
}elseif( $page+6 >= $obpag and $page != $obpag and $page != 1){
    $pleft .= '&nbsp;<span class="navi"><a 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;<span class="navi"><a href= '.$domen.'/index.php?'.$upag.'&amp;n='. ($page + $i) .'>'. ($page + $i) .'</a></span>';}
    }

    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;<span class="navi"><a href= '.$domen.'/index.php?'.$upag.'&amp;n='. ($page - $ops + $i) .'>'. ($page - $ops + $i) .'</a></span>';}
    }
}elseif($page == $obpag){
    $pleft .= '&nbsp;<span class="navi"><a href= '.$domen.'/index.php?'.$upag.'&amp;n=1>1</a></span>&nbsp;...';

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

echo $pnaz.$pleft.'&nbsp;<span class="vnavi">'.$page.'</span>'.$pright.$pdal;
?>