<?php
/**
* Пагинация
*/
class pagination
{
public
/** @var int
*/
$page,
/** @var int
*/
$pages,
/** @var int
*/
$_at;
/**
* Экземпляр
*/
function __construct($items_count = 0, $items_per_page = 10)
{
$this->page = intval($_GET['page']); //
if ($this->page < 1)
$this->page = 1;
$this->pages= ceil($items_count/$items_per_page);
//
if ($_GET['page'] == 'end' || $this->page > $this->pages)
$this->page = $this->pages;
$this->_at = $this->page * $items_per_page - $items_per_page;
}
/**
* Отображение
*/
public function display($ref = '')
{
// Первая страница
if ($this->page == 1)
$output .= '1';
if ($this->page != 1)
$output .= '<a href="' . $ref . 'page=1">1</a> ';
// Наш паравозик со страницами
for ($i = -3; $i <= 3; $i++)
{
if (($this->page + $i) > 1 && ($this->page + $i) < $this->pages)
{
//
if ($i == -3 && ($this->page + $i) > 2)
$output .= '..';
//
if (($this->page + $i) > 0)
$output .= (($this->page + $i) == $this->page) ? $this->page : ' <a href="' . $ref . 'page=' . ($this->page + $i) . '">' . ($this->page + $i) . '</a> ';
//
if ($i == 3 && ($this->page + $i) < ($this->pages - 1))
$output .= '..';
}
}
// Последняя страница
if ($this->page != $this->pages)
$output .= ' <a href="' . $ref . 'page=end">' . $this->pages . '</a>';
if ($this->page == $this->pages)
$output .= $this->pages;
echo $output;
}
}