Класс пагинации для Битвы титанов
Пример:
class pagination {
private $page, $pages, $items, $items_per_page;
public function __construct ($items,
$items_per_page) {
$this -> items = $items;
$this -> items_per_page = $items_per_page;
$this -> pages = $this -> get_pages ();
$this -> page = intval ($_GET['page']);
if ($this -> page < 1) {
$this -> page = 1;
}
if ($this -> page > $this -> pages) {
$this -> page = $this -> pages;
}
}
public function get_pages () {
return ceil ($this -> items / $this -> items_per_page);
}
public function display () {
if ($this -> page > 1) {
$output .= "<a href='?page=1'><<</a> ";
$output .= "<a href='?page=" . ($this -> page - 1) . "'><</a> ";
}
for ($page = $this -> page - 2;
$page<= $this -> page + 2;
$page++) {
if ($page > 0 && $page <= $this -> pages) {
if ($page == $this -> page) {
$output .= $page . " ";
}
else {
$output .= "<a href='?page=$page'>$page</a> ";
}
}
}
if ($this -> page + 1 <= $this -> pages) {
$output .= " <a href='?page=" . ($this -> page + 1) . "'>></a>";
}
if ($this -> page != $this -> pages) {
$output .= " <a href='?page=" . $this -> pages . "'>>></a>";
}
return $output;
}
}
Пример:
$pagination = new pagination (100, 10);
// ...
echo $pagination -> display ();