Размер файла: 5.28Kb
<?php
class Paginator
{
public function __construct($records, $on_page, $link = '?')
{
/* Всего записей */
$this->all_records = $records;
$this->link = $link;
$this->on_page = $on_page < 1 ? 1 : $on_page;
$this->all_pages = ceil($this->all_records / $this->on_page);
$this->page = !isset($_GET['page']) || $_GET['page'] <= 0 ? 1 : abs(intval($_GET['page']));
$this->page = $this->page > $this->all_pages ? $this->all_pages : $this->page;
$this->page = isset($_GET['last']) ? $this->all_pages : $this->page;
$this->start = ($this->page * $this->on_page) - 1;
$this->start = $this->start > $this->all_records ? $this->all_records : $this->start;
$this->end = ($this->page * $this->on_page)- $this->on_page;
$this->last_three_page = $this->all_pages - 2;
$this->lt = '← ';
$this->gt = ' →';
}
public function pagination()
{
$string_pages = null;
if($this->all_pages > 10)
{
if($this->page <= 3)
{
for($i=1; $i <= 3; $i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
if($this->page == 3)
{
$string_pages .= ' <a href="'.$this->link.'page=4">4</a> ';
}
$string_pages .= '...';
for($i = $this->last_three_page; $i <= $this->all_pages; $i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
}
elseif($this->page < 6 && $this->page <= $this->last_three_page)
{
for($i=1;$i<=6;$i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
$string_pages .= '...';
for($i = $this->last_three_page; $i <= $this->all_pages; $i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
}
elseif($this->page >= 6 && $this->page <= ($this->last_three_page - 2))
{
for($i=1;$i<=3;$i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
$string_pages .= '...';
$string_pages .= ' <a href="'.$this->link.'page='.($this->page - 1).'">'.($this->page - 1).'</a> ';
$string_pages .= ' '.$this->page.' ';
$string_pages .= ' <a href="'.$this->link.'page='.($this->page + 1).'">'.($this->page + 1).'</a> ';
$string_pages .= '...';
for($i = ($this->last_three_page); $i <= $this->all_pages; $i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
}
elseif(($this->page + 1) >= ($this->last_three_page))
{
for($i=1; $i <= 3; $i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
$string_pages .= '...';
for($i = ($this->last_three_page - 3); $i <= $this->all_pages; $i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
}
}
else
{
for($i=1;$i<=$this->all_pages;$i++)
{
$string_pages .= $this->page == $i ? ' '.$i.' ' : ' <a href="'.$this->link.'page='.$i.'">'.$i.'</a> ';
}
}
return $string_pages;
}
public function back_forward_links($next, $back, $razdelitel = ' | ' , $poradok = true)
{
if($poradok === true)
{
if($this->page <= 1)
{
$this->this_links = '<a href="'.$this->link.'page='.($this->page+1).'">'.$next.$this->gt.'</a>';
}
elseif($this->page >= $this->all_pages)
{
$this->this_links = '<a href="'.$this->link.'page='.($this->page-1).'">'.$this->lt.$back.'</a>';
}
else
{
$this->this_links = '<a href="'.$this->link.'page='.($this->page-1).'">'.$this->lt.$back.'</a>'.$razdelitel.'<a href="'.$this->link.'page='.($this->page+1).'">'.$next.$this->gt.'</a>';
}
}
else
{
if($this->page <= 1)
{
$this->this_links = '<a href="'.$this->link.'page='.($this->page+1).'">'.$back.$this->gt.'</a>';
}
elseif($this->page >= $this->all_pages)
{
$this->this_links = '<a href="'.$this->link.'page='.($this->page-1).'">'.$this->lt.$next.'</a>';
}
else
{
$this->this_links = '<a href="'.$this->link.'page='.($this->page-1).'">'.$this->lt.$next.'</a>'.$razdelitel.'<a href="'.$this->link.'page='.($this->page+1).'">'.$back.$this->gt.'</a>';
}
}
return $this->this_links;
}
public function show_form($value = 'GO', $array = array())
{
$hidden = null;
if(count($array) > 0)
{
foreach($array as $key => $value)
{
$hidden .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
}
}
return '<form action="" method="get">'.
$hidden.
'<input type="text" size="2" name="page" value="'.$this->page.'" />'.
'<input type="submit" value="'.$value.'" />'.
'</form>';
}
function limit()
{
return 'LIMIT '.(($this->page*$this->on_page)-$this->on_page).', '.$this->on_page;
}
function count()
{
if($this->all_records > $this->on_page)
{
return true;
}
else
{
return false;
}
}
}