Класс для постраничного вывода данных из mysql в браузер — PHP

Класс pagination является инструментом, который позволит Вам разбивать большие наборы результатов на несколько страниц

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
$paginationObj = new pagination(total_limit);
$limit = $paginationObj->paginationGetLimit();
  
mysql query(variable $limit goes here)
$paginationObj->paginationCreatePages();
?>
Значения по умолчанию:
общий лимит (total_limit) = 500
результатов на странице = 30
страниц с номерами = 5
Примечание:
Работает только с динамическими url ввида: samplecode.ru/script.php?page=num или script.php?переменная=значение&страница=номер и т. п.
 
code: #php
class pagination
 {
  
   private $max_rows = 30;//MAXIMUM NUMBER OF DISPLAYING ITEMS
   private $max_num = 5;//MAXIMUM NUMBER OF NUMERIC LINKS
   private $limit;
   private $maxId;//TOTAL NUMBER OF ITEMS
   private $lastpage;
   private $page;
   private $url;
   private $match = "page=";
  
  
 public function __construct($maxId = null)
 {
   if(!$maxId)
   {
     $this->maxId = 500;
   }
   else
   {
     $this->maxId = $maxId;
   }
 }
  
  
 public function getPage()
 {
   return $this->page;
 }
  
  
 public function paginationGetLimit()
 {
   $this->page = isset($_GET['page']) ? strip_tags($_GET['page']) : 1;
   $this->lastpage = ceil($this->maxId / $this->max_rows);
   $this->page = (int)$this->page;
  
   if($this->page < 1)
   {
     $this->page = 1;
   }
   elseif($this->page > $this->lastpage)
   {
     $this->page = $this->lastpage;
   }
  
   return ($this->limit = 'LIMIT ' .($this->page - 1) * $this->max_rows .',' .$this->max_rows);
 }
  
  
 public function paginationCreatePages()
 {
    
   $this->url = $_SERVER['REQUEST_URI'];//THE REQUESTED URL
   $pos = strpos($this->url, $this->match);
   .
   echo "<div class='pagination'>";
   echo "<ul>";
   //NEXT ENTRIES
   if ($this->page == 1)
   {
     //do nothing
   }
   else
   {
     $prevpage = $this->page-1;
  
     if($pos != '')
     {
       $nextUrl = str_replace($this->match.$this->page, $this->match.$prevpage, $this->url);
  
       echo "<li>";
       echo "<a href='".$nextUrl."'>&laquo;</a>";
       echo "</li> ";
     }
     else
     {
       print "The document can not create pages";
       return;
     }
   }
   //MIDDLE PAGES
   if($this->lastpage != 1)
   {
     $max_links = $this->max_num + 1;
     $h = 1;
  
     if($this->page > $max_links)
     {
       $h = (($h + $this->page) - $max_links);
     }
  
     if($this->page >= 1)
     {
       $max_links = $max_links + ($this->page-1);
     }
  
     if($max_links > $this->lastpage)
     {
       $max_links = $this->lastpage + 1;
     }
  
     for($i=$h; $i<$max_links; $i++)
     {
       if ($i == $this->page)
       {
         echo "<li id='f'>";
         echo $i;
         echo "</li>";
       }
       else
       {
         if($pos == '')
         {
           if(strpos($this->url, '?') != '')
           {
             $specialChar = "&amp;";
           }
           else
           {
             $specialChar = "?";
           }
  
           $currentUrl = $this->url.$specialChar.$this->match.$i;
         }
         else
         {
           $currentUrl = str_replace($this->match.$this->page, $this->match.$i, $this->url);
         }
  
         echo "<li>";
         echo "<a href='".$currentUrl."'>$i</a>";
         echo "</li> ";
       }
     }
   }
   //PREVIOUS ENTRIES
   if ($this->page == $this->lastpage)
   {
     //do nothing
   }
   else
   {
     $nextpage = $this->page + 1;
  
     if($pos == '')
     {
       if(strpos($this->url, '?') != '')
       {
         $specialChar = "&amp;";
       }
       else
       {
         $specialChar = "?";
       }
  
       $prevUrl = $this->url.$specialChar.$this->match.$nextpage;
     }
     else
     {
       $prevUrl = str_replace($this->match.$this->page, $this->match.$nextpage, $this->url);
     }
  
     echo "<li>";
     echo "<a href='".$prevUrl."'>&raquo;</a>";
     echo "</li> ";
   }
    
   echo "</ul>";
   echo "</div>";
 }
  
  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
.pagination
 {
   width:100%;
   margin-top:20px;
   margin-left:10px;
   clear:left
 }
  
 .pagination ul
 {
   list-style-type: none;
   margin:0;
   padding:0;
 }
  
 .pagination ul li
 {
   color:#666666;
   float:left;
   font: Eras Bold ITC;
   font-size: 12px;
   letter-spacing: .01em;
 }
  
 .pagination ul li a
 {
   color: #47809E;
   display: block;
   margin: 0 0.1em;
   padding: 2px;
   padding-left: 4px;
   padding-right: 4px;
   text-decoration: none;
 }
  
 li#f
 {
   background-color:#fff;
   display: block;
   margin: 0 0.1em;
   padding: 2px;
   padding-left: 4px;
   padding-right: 4px;
   text-decoration: none;
   color:#666666;
 }
  
 .pagination ul li a:hover
{
   text-decoration:underline;
 }