Вход Регистрация
* Wins

Постраничка by Wins

  1. <?php
  2.  
  3. class Pagination {
  4.  
  5. public $total = 0;
  6. public $per_page = 10;
  7. public $start = 0;
  8. public $query = NULL;
  9. public $view = FALSE;
  10.  
  11. private $page = 1;
  12. private $pages = 1;
  13.  
  14.  
  15. public function run() {
  16.  
  17. if($this->total <= $this->per_page) return FALSE;
  18.  
  19. $this->pages = ceil($this->total / $this->per_page);
  20.  
  21. if(!empty($_GET['page']) && $_GET['page'] = intval($_GET['page'])) {
  22.  
  23. $this->page = abs($_GET['page']);
  24.  
  25. if($this->page > $this->pages) $this->page = $this->pages;
  26.  
  27. $this->start = $this->per_page * $this->page - $this->per_page;
  28.  
  29. }
  30.  
  31.  
  32. if($this->page > 1) {
  33.  
  34. $page1 = $this->page - 1;
  35. $page2 = $this->page - 2;
  36.  
  37. if($this->page > 3)
  38. $this->view .= ' <a href="'. $this->query .'1">&lt;&lt;</a> ';
  39.  
  40. $this->view .= ' <a href="'. $this->query . $page1 .'">&lt;</a> ';
  41.  
  42. if($this->page > 2)
  43. $this->view .= ' <a href="'. $this->query . $page2 .'">'. $page2 .'</a> |';
  44.  
  45. $this->view .= ' <a href="'. $this->query . $page1 .'">'. $page1 .'</a> |';
  46.  
  47. }
  48.  
  49.  
  50. $this->view .= ' <b>'. $this->page .'</b>';
  51.  
  52.  
  53. if($this->page < $this->pages) {
  54.  
  55. $page1 = $this->page + 1;
  56. $page2 = $this->page + 2;
  57.  
  58. $this->view .= ' | <a href="'. $this->query . $page1 .'">'. $page1 .'</a>';
  59.  
  60. if($page1 < $this->pages)
  61. $this->view .= ' | <a href="'. $this->query . $page2 .'">'. $page2 .'</a>';
  62.  
  63. $this->view .= ' <a href="'. $this->query . $page1 .'">&gt;</a>';
  64.  
  65. if($page2 < $this->pages)
  66. $this->view .= ' <a href="'. $this->query . $this->pages .'">&gt;&gt;</a>';
  67.  
  68. }
  69.  
  70. $this->view = '<pre>'. $this->view .'</pre>';
  71.  
  72. return TRUE;
  73.  
  74. }
  75.  
  76. }
  77.  
  78. ?>
» Описание: Писал недавно для себя, мб кому пригодится.
Все просто и минималистично.

Пример использования:

<?php

require_once 'pagination.php';

$pg = new Pagination;

$pg->total = mysql_result(mysql_query("SELECT COUNT(*) FROM table"), 0); // Счетчик записей
$pg->per_page = 10; // Записей на страницу
$pg->query = '/file.php?page='; // Адрес для навигации

$pg->run();

$query = mysql_query("SELECT * FROM table ORDER BY column ASC LIMIT $pg->start, $pg->per_page");

while($result = mysql_fetch_assoc($query)) {

echo $result['column'] .'<br>';

}

if($pg->view) echo $pg->view;

?>
» Время добавления: 27 Сент 2016 в 03:17
» Посмотров: 3210
» textarea
» Рейтинг: [+3 | -2]
Комментарии [6]
Онлайн: 1
Реклама