<?php
//класс навигации
class N{
public $post;
public $start;
public $num;
public $limit;
public $page=1;
private $who;
private $hvost;
private $total=0;
############################
public function __construct($post,$num,$who,$hvost)
{
if(isset($_REQUEST['page']))
{
$this->page=intval($_REQUEST['page']);
}
if(is_numeric($post))
{
$this->post=$post;
}else
{
$this->post=mysql_result(mysql_query($post),0);
}
if($this->post)
{
$this->who=$who;
empty($this->hvost) || $this->hvost == '?' ? $this->hvost=$hvost : $this->hvost=$hvost.'&' ;
//!empty($this->hvost) || $this->hvost != '?' ? $this->hvost=$hvost.'&' : $this->hvost=$hvost;
$this->num=$num;
$this->total=intval(($this->post-1)/$this->num)+1;
if($this->page>$this->total)
{
$this->page=$this->total;}elseif($this->page<1){$this->page=1;
}
$this->start=$this->page*$this->num-$this->num;
$this->limit=$this->start.', '.$this->num;
}
}
##############
public function __destruct(){
$pp=$p2l=$p1l=$p1r=$p2r=$np='';
if($this->page<$this->total){
$p1r='<a href="'.$this->hvost.'page='.($this->page+1).'">'.($this->page+1).'</a>';
if($this->page+1!=$this->total and $this->total-4<$this->page){$p1r.=', ';}
}
if($this->page+4<$this->total){
$p1r.=', <a href="'.$this->hvost.'page='.($this->page+2).'">'.($this->page+2).'</a>';
}
if($this->page-3>0){
$pp='<a href="'.$this->hvost.'page=1'.'">1</a>';
if($this->page-4>0){
$pp.=', <a href="'.$this->hvost.'page=2'.'">2</a>';
}
if($this->page-5>0){
$pp.=', <a href="'.$this->hvost.'page=3'.'">3</a>';
}
if($this->page-6>0 and $this->page<8){
$pp.=', <a href="'.$this->hvost.'page=4"'.'>4</a>';
}
if($this->page>7){$pp.='...';}else{$pp.=', ';}
}
if($this->page+2<$this->total){
$np='<a href="'.$this->hvost.'page='.$this->total.'">'.$this->total.'</a>';
}
if($this->page-2>0){
$p2l='<a href="'.$this->hvost.'page='.($this->page-2).'">'.($this->page-2).'</a>, ';
}
if($this->page-1>0){
$p1l='<a href="'.$this->hvost.'page='.($this->page-1).'">'.($this->page-1).'</a>, ';
}
if($this->total-3>$this->page){
if($this->page+3==$this->total-3){
$p2r=', <a href="'.$this->hvost.'page='.($this->total-3).'">'.($this->total-3).'</a>';
}
if($this->page+2<$this->total-4){$p2r.='...';}else{$p2r.=', ';}
$p2r.='<a href="'.$this->hvost.'page='.($this->total-2).'">'.($this->total-2).'</a>, <a href="'.$this->hvost.'page='.($this->total-1).'">'.($this->total-1).'</a>, ';
}elseif($this->page+1<$this->total){
$p2r='<a href="'.$this->hvost.'page='.($this->page+2).'">'.($this->page+2).'</a>';
if($this->page+2<$this->total){$p2r.=', ';}
}
#######
if ($this->total != 1 && $this->total != 0)
{
if($this->page!=1)
{
echo '<a href="'.$this->hvost.'page='.($this->page-1).'">«</a> ';
}
if($this->total!=1){
echo $pp.$p2l.$p1l.' <b>'.$this->page.'</b>, '.$p1r.$p2r.$np;
}
if($this->page<$this->total){
echo ' <a href="'.$this->hvost.'page='.($this->page+1).'">»</a>';
}
if($this->total>10){
echo '<form class="nav" action="'.$this->hvost.'" method="post">
Стр.: <input type="text" name="page" size="3" maxlength="3" value="'.$this->page.'" />
<input type="submit" value=">>" />
</form>';
}
}
}
}