Hej!
Splodzilem nowa, mala, klase sluzaca do przestawiania pagera.
Oto jej kod zrodlowy:
<?php
class Pager
{
private $total;
private $current;
private $pageHandler;
private $formats = Array( 'main' => '<a href="%s">%d</a>', // glowny link
'mainActive' => '<b>%d</b>', // numer aktualnej strony
'begin' => '<a href="%s">|<</a>', // powrot na poczatek
'beginUnActive' => '|<', // nieaktywny powrot na poczatek
'end' => '<a href="%s">>|</a>', // przeskok na koniec
'endUnActive' => '>|', // nieaktywny przeskok na koniec
'next' => '<a href="%s">>></a>', // nastepna strona
'nextUnActive' => '>>', // nieaktywna nastepna strona
'previous' => '<a href="%s"><<</a>', // poprzednia strona
'previousUnActive' => '<<', // nieaktywna poprzednia strona
'pause' => '...', // pauza miedzy czescia lewa, glowna, prawa
'separator' => ' ' // separator poszczegolnych elementow
);
'leftShift' => 2, // wypisywanie x liczb z poczatku
'mainShift' => 3, // wypisywanie x liczb z lewej i prawej strony aktualnej
'rightShift' => 2 // wypisywanie x liczb na koncu
);
public function __construct($total, $pageHandler = 'page')
{
if (isset($_GET[$pageHandler]) && (int
)$_GET[$pageHandler] < 1
|| !isset($_GET[$pageHandler])) $current = 1;
elseif (isset($_GET[$pageHandler]) && (int
)$_GET[$pageHandler] > $total) $current = $total;
else
$current = (int)$_GET[$pageHandler];
$this->total = $total;
$this->current = $current;
$this->pageHandler = $pageHandler;
}
private function replacePage($new)
{
if (eregi($this->pageHandler."=", $_SERVER['REQUEST_URI'])) return str_replace($this->pageHandler."=".$this->current, $this->pageHandler."=".$new, $_SERVER['REQUEST_URI']); elseif (count($_SERVER['argv']) == 0
) return $_SERVER['REQUEST_URI']."?".$this->pageHandler."=".$new;
else
return $_SERVER['REQUEST_URI']."&".$this->pageHandler."=".$new;
}
public function createPager()
{
$arrayTemp[] = ($this->current > 1
) ?
sprintf($this->formats['begin'], $this->replacePage(1
)) : sprintf($this->formats['beginUnActive']);
$arrayTemp[] = ($this->current > 1
) ?
sprintf($this->formats['previous'], $this->replacePage($this->current - 1
)) : sprintf($this->formats['previousUnActive']);
if ($this->config['leftShift'] + $this->config['mainShift'] + $this->config['rightShift'] >= $this->total)
for ($i = 1; $i <= $this->total; $i++)
$arrayTemp[] = ($i == $this->current) ?
sprintf($this->formats['mainActive'], $i) : sprintf($this->formats['main'], $this->replacePage($i), $i); else
{
$pauseBefore = $pauseAfter = FALSE;
$left = 1 + $this->config['leftShift'];;
$mainLeft = ($this->current - $this->config['mainShift'] >= 1) ? $this->current - $this->config['mainShift'] : 1;
$mainRight = ($this->current + $this->config['mainShift'] <= $this->total) ? $this->current + $this->config['mainShift'] : $this->total;
$right = $this->total - $this->config['rightShift'];
$start = ($right < $mainRight) ? $mainRight + 1 : $right + 1;
$stop = ($left >= $mainLeft) ? $mainLeft : $left;
for ($i=1; $i<$stop; $i++)
{
$arrayTemp[] = sprintf($this->formats['main'], $this->replacePage($i), $i); $pauseBefore = TRUE;
}
if ($pauseBefore == TRUE)
$arrayTemp[] = sprintf($this->formats['pause']);
for ($i = $mainLeft; $i<=$mainRight; $i++)
$arrayTemp[] = ($i == $this->current) ?
sprintf($this->formats['mainActive'], $i) : sprintf($this->formats['main'], $this->replacePage($i), $i);
$arrayTemp[] = sprintf($this->formats['pause']);
for ($i=$start; $i <= $this->total; $i++)
{
$arrayTemp[] = sprintf($this->formats['main'], $this->replacePage($i), $i); $pauseAfter = TRUE;
}
if ($pauseAfter != TRUE)
unset($arrayTemp[(count($arrayTemp) - $this->config['rightShift'] + 1
)]); }
$arrayTemp[] = ($this->current + 1
<= $this->total) ?
sprintf($this->formats['next'], $this->replacePage($this->current + 1
)) : sprintf($this->formats['nextUnActive']);
$arrayTemp[] = ($this->current < $this->total) ?
sprintf($this->formats['end'], $this->replacePage($this->total)) : sprintf($this->formats['endUnActive']);
return implode($this->formats['separator'], $arrayTemp); }
}
?>
Chcialem, aby calosc byla jak najprostsza w pozniejszym uzyciu i tak wykorzystanie klasy sprowadza sie do:
<?php
$pager = new Pager(20);
echo $pager->createPager(); ?>
Pierwszym parametrem jest liczba wszystkich stron, drugi (opcjonalny) parametr to zmienna sluzaca do przechowywania numeru strony (domyslnie: page).
Klasa generuje 2 rodzaje pagera:
- jesli liczba stron jest mniejsza od sumy zakresow z configa:
|< << 1 2 3 4 5 6 >> >|
- lub w przeciwnym wypadku ucina liste (wyobrazmy sobie np. wypisane jak powyzej linki np. do 100 stron

):
|< << 1 2 ... 9 10 11 12 13 14 15 ... 19 20 >> >|
Prosilbym o ocene tej klasy (dopiero zaczynam przygode z OOP) oraz o pomoc: w momencie gdy recznie w adresie przegladarki ustalimy page na wieksza niz podana w konstruktorze skrypt zachowuje sie dziwnie: kazdy link prowadzi do tejze podstrony... Przeciez jest warunek w konstruktorze... Poza tam odwolanie do $_GET jest tylko w 1 miejscu. Prosilbym o spojrzenie na ten bug... Z gory dzieki!