1 | <?
|
---|
2 | class CleverPager
|
---|
3 | {
|
---|
4 | // SQL prikaz pro vyber dat pred strankovanim,
|
---|
5 | // nesmi pouzivat LIMIT
|
---|
6 | public $SelectCommand;
|
---|
7 | // SQL prikaz na vypocitani poctu zaznamu lze nechat
|
---|
8 | // nenastaveny, v tom pripade se vygeneruje v metode DataBind
|
---|
9 | public $CountCommand;
|
---|
10 | // Pocet zaznamu na strance
|
---|
11 | public $PageSize = 10;
|
---|
12 | // Parametr URL, ze ktereho se cte cislo akt. stranky
|
---|
13 | public $UrlParameterName;
|
---|
14 | // atribut ID, ktery bude mit div kolem pageru
|
---|
15 | public $PagerID = "PagerId";
|
---|
16 | // atribut Class, ktery bude mit div kolem pageru
|
---|
17 | public $PagerCssClass = "PagerClass";
|
---|
18 | // zarovnani - hodnota text-align v atributu style,
|
---|
19 | // hodnota inherit znamena, ze div tento atribut
|
---|
20 | // vubec mit nebude
|
---|
21 | public $PagerAlign = "inherit";
|
---|
22 | // pocet cisel okolnich stranek, ktere se zobrazi
|
---|
23 | // hodnota -1 znamena, ze se zobrazi vsechny
|
---|
24 | // hodnota 0 znamena zadne
|
---|
25 | public $PageLinksDisplayed = -1;
|
---|
26 | // priznak zobrazovani tlacitek predchozi a nasledujici stranky
|
---|
27 | public $ShowPrevNext = 1;
|
---|
28 | // priznak zobrazovani tlacitek prvni a posledni stranky
|
---|
29 | public $ShowFirstLast = 1;
|
---|
30 |
|
---|
31 | // text odkazu na prvni stranku
|
---|
32 | public $FirstPage = "<<";
|
---|
33 | // text odkazu na posledni stranku
|
---|
34 | public $LastPage = ">>";
|
---|
35 | // text odkazu na predchozi stranku
|
---|
36 | public $PrevPage = "<";
|
---|
37 | // text odkazu na nasledujici stranku
|
---|
38 | public $NextPage = ">";
|
---|
39 |
|
---|
40 | // css tridy, ktere budou mit jednotlive odkazy na stranky
|
---|
41 | public $LinkCssClass;
|
---|
42 | public $FirstCssClass;
|
---|
43 | public $LastCssClass;
|
---|
44 | public $NextCssClass;
|
---|
45 | public $PrevCssClass;
|
---|
46 | public $CurrentPageCssClass;
|
---|
47 |
|
---|
48 | // aktualni stranka
|
---|
49 | private $_targetedPage = 2;
|
---|
50 | // pocet stranek
|
---|
51 | private $_pageCount = 0;
|
---|
52 | // Zde bude vysledkova sada po zavolani DataBind
|
---|
53 | private $ResultSet = NULL;
|
---|
54 | // priznak prvniho parametru, nedulezite
|
---|
55 | private $first = 0;
|
---|
56 |
|
---|
57 | // konstruktor - parametry jsou SQL prikaz na vyber vysledkove
|
---|
58 | // sady a parametr URL, ve kterem bude cislo aktualni stranky
|
---|
59 | public function __construct(
|
---|
60 | $SelectCommand,
|
---|
61 | $UrlParameterName)
|
---|
62 | {
|
---|
63 | $this->SelectCommand = $SelectCommand;
|
---|
64 | $this->UrlParameterName = $UrlParameterName;
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Spocita pocet zaznamu a z atributu _pageCount,
|
---|
68 | // PageSize a _targetedPage vybere pozadovanou stranku
|
---|
69 | public function DataBind()
|
---|
70 | {
|
---|
71 | // priprava prikazu pro spocitani zaznamu,
|
---|
72 | // pokud nebyl zadan zvenci
|
---|
73 | if (!$this->CountCommand || $this->CountCommand == "")
|
---|
74 | {
|
---|
75 | $this->CountCommand = preg_replace(
|
---|
76 | '@SELECT (.*) FROM@',
|
---|
77 | 'SELECT COUNT(*) AS Count FROM',
|
---|
78 | $this->SelectCommand);
|
---|
79 | }
|
---|
80 | //zjisteni cisla aktualni stranky
|
---|
81 | if (isset($_GET[$this->UrlParameterName]))
|
---|
82 | $this->_targetedPage =
|
---|
83 | (int)($_GET[$this->UrlParameterName]);
|
---|
84 | else
|
---|
85 | $this->_targetedPage = 1;
|
---|
86 | if ($this->_targetedPage == 0)
|
---|
87 | $this->_targetedPage = 1;
|
---|
88 | //zjisteni poctu zaznamu
|
---|
89 | $c = (mysql_fetch_object(
|
---|
90 | mysql_query($this->CountCommand)));
|
---|
91 | //vypocet poctu stranek
|
---|
92 | $this->_pageCount =
|
---|
93 | (int) (($c->Count + $this->PageSize - 1) / $this->PageSize);
|
---|
94 | if ($this->_targetedPage > $this->_pageCount)
|
---|
95 | $this->_targetedPage = 1;
|
---|
96 | //vypocet parametru pro LIMIT
|
---|
97 | $firstrow =
|
---|
98 | ($this->_targetedPage - 1) * $this->PageSize;
|
---|
99 | $this->ResultSet =
|
---|
100 | mysql_query("$this->SelectCommand ".
|
---|
101 | " LIMIT $firstrow, $this->PageSize" ) or die($this->SelectCommand.'<br />'.mysql_error());
|
---|
102 | }
|
---|
103 |
|
---|
104 | // postupne vraci radky vysledkove sady,
|
---|
105 | // po zavolani DataBind
|
---|
106 | public function GetOne()
|
---|
107 | {
|
---|
108 | return mysql_fetch_array($this->ResultSet);
|
---|
109 | }
|
---|
110 |
|
---|
111 | //pomocna metoda pro vytvoreni odkazu
|
---|
112 | private function prepareLink(
|
---|
113 | $prefix,
|
---|
114 | $page,
|
---|
115 | $anchoringText,
|
---|
116 | $cssClass)
|
---|
117 | {
|
---|
118 | if ($page != $this->_targetedPage)
|
---|
119 | {
|
---|
120 | if ($page == 1)
|
---|
121 | $h = $prefix;
|
---|
122 | else {
|
---|
123 | if ($this->first)
|
---|
124 | $h = $prefix."?".$this->UrlParameterName."=".$page;
|
---|
125 | else
|
---|
126 | $h = $prefix."&".$this->UrlParameterName."=".$page;
|
---|
127 | }
|
---|
128 | if ($cssClass != "" && $cssClass)
|
---|
129 | $css = " class=\"$cssClass\"" ;
|
---|
130 | else $css="";
|
---|
131 | return "<a href=\"$h\"$css>$anchoringText</a> ";
|
---|
132 | } else {
|
---|
133 | if ($this->CurrentPageCssClass &&
|
---|
134 | $this->CurrentPageCssClass != "")
|
---|
135 | {
|
---|
136 | echo "<span class=\"$this->CurrentPageCssClass\">";
|
---|
137 | echo $anchoringText;
|
---|
138 | echo "</span> ";
|
---|
139 | }
|
---|
140 | else
|
---|
141 | echo $anchoringText." ";
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | // vypise pager do tela stranky
|
---|
146 | public function DrawPager()
|
---|
147 | {
|
---|
148 | //analyza url
|
---|
149 | $hrefprefix = $_SERVER['SCRIPT_NAME'];
|
---|
150 | $this->first=1;
|
---|
151 | foreach ( $_GET as $key=>$value) {
|
---|
152 | if ($key == $this->UrlParameterName)
|
---|
153 | continue;
|
---|
154 | if ($this->first)
|
---|
155 | {
|
---|
156 | $hrefprefix .= "?".$key."=".$value;
|
---|
157 | $this->first = 0;
|
---|
158 | }
|
---|
159 | else
|
---|
160 | $hrefprefix .= "&".$key."=".$value;
|
---|
161 | }
|
---|
162 |
|
---|
163 | //vykresleni div tagu kolem pageru s odpovidajicimi atributy
|
---|
164 | if ($this->PagerID && $this->PagerID != "")
|
---|
165 | $id = " id=\"$this->PagerID\"";
|
---|
166 | else $id ="";
|
---|
167 | if ($this->PagerCssClass && $this->PagerCssClass != "")
|
---|
168 | $class = " class=\"$this->PagerCssClass\"";
|
---|
169 | else $class = "";
|
---|
170 | if ($this->PagerAlign != "inherit")
|
---|
171 | $align = " style=\"text-align: $this->PagerAlign\"";
|
---|
172 | else
|
---|
173 | $align ="";
|
---|
174 | echo "<div $id $class $align >";
|
---|
175 |
|
---|
176 | //prvni stranka
|
---|
177 | if ($this->ShowFirstLast)
|
---|
178 | {
|
---|
179 | echo $this->prepareLink(
|
---|
180 | $hrefprefix,
|
---|
181 | 1,
|
---|
182 | $this->FirstPage,
|
---|
183 | $this->FirstCssClass);
|
---|
184 | }
|
---|
185 |
|
---|
186 | //predchozi stranka
|
---|
187 | if ($this->ShowPrevNext)
|
---|
188 | {
|
---|
189 | $p = max($this->_targetedPage - 1, 1);
|
---|
190 | echo $this->prepareLink(
|
---|
191 | $hrefprefix,
|
---|
192 | $p,
|
---|
193 | $this->PrevPage,
|
---|
194 | $this->PrevCssClass);
|
---|
195 | }
|
---|
196 |
|
---|
197 | //stranky mezi bez omezeni
|
---|
198 | if ($this->_pageCount <= $this->PageLinksDisplayed ||
|
---|
199 | $this->PageLinksDisplayed == -1)
|
---|
200 | {
|
---|
201 | for ($p = 1; $p <= $this->_pageCount; $p++)
|
---|
202 | echo $this->prepareLink(
|
---|
203 | $hrefprefix,
|
---|
204 | $p,
|
---|
205 | $p,
|
---|
206 | $this->LinkCssClass);
|
---|
207 | }
|
---|
208 | else //stranky mezi
|
---|
209 | {
|
---|
210 | $pagesBefore = (int)min(
|
---|
211 | $this->PageLinksDisplayed / 2,
|
---|
212 | $this->_targetedPage - 1);
|
---|
213 | $pagesAfter = (int)min(
|
---|
214 | $this->PageLinksDisplayed - $pagesBefore - 1,
|
---|
215 | $this->_pageCount - $this->_targetedPage);
|
---|
216 | if ($pagesAfter + $pagesBefore + 1 != $this->PageLinksDisplayed)
|
---|
217 | $pagesBefore = $this->PageLinksDisplayed - 1 - $pagesAfter;
|
---|
218 | for ($p = $this->_targetedPage - $pagesBefore;
|
---|
219 | $p <= $this->_targetedPage + $pagesAfter;
|
---|
220 | $p++)
|
---|
221 | echo $this->prepareLink($hrefprefix, $p, $p, $this->LinkCssClass);
|
---|
222 | }
|
---|
223 |
|
---|
224 | //nasledujici stranka
|
---|
225 | if ($this->ShowPrevNext)
|
---|
226 | {
|
---|
227 | $p = min($this->_targetedPage + 1, $this->_pageCount);
|
---|
228 | echo $this->prepareLink(
|
---|
229 | $hrefprefix,
|
---|
230 | $p,
|
---|
231 | $this->NextPage,
|
---|
232 | $this->NextCssClass);
|
---|
233 | }
|
---|
234 |
|
---|
235 | //posledni stranka
|
---|
236 | if ($this->ShowFirstLast)
|
---|
237 | {
|
---|
238 | $p = $this->_pageCount;
|
---|
239 | echo $this->prepareLink(
|
---|
240 | $hrefprefix,
|
---|
241 | $p,
|
---|
242 | $this->LastPage,
|
---|
243 | $this->LastCssClass);
|
---|
244 | }
|
---|
245 |
|
---|
246 | //uzavreni bloku pageru
|
---|
247 | echo "</div>";
|
---|
248 | }
|
---|
249 | }
|
---|
250 | ?>
|
---|