Z potrzeby - bo to własnie ona jest matką wynalazków (a tak można nazwać ową klasę) - napisałem dla Siebie, swoich projektów prostą klasę do obsługi stronicowania. Przy projektowaniu skupiłem się głównie na tym aby generując nowy Pager mieć jak największą swobode przy generowaniu nowego pagera. Wszystkie parametry jakie uznałem za warte zastosowania można więc zmienic wywolujac klase. Przygotowalem ja tez dla kilku wywolan na stronie.
Klasa stylizowana jest na PHP5 - określenie iż jest napisana w PHP5 bym nie urzyl..
Jest to moja pierwsza publikowana klasa, kod nie jest opisany, ale z czasem poprawie zarówno klase jak i opisze ja zgodnie ze standardami.
Zapraszam do testowania :
<? ############################################################### # Adrian Staniszewski # adrian_s@toya.net.pl # GG: 5513236 # # File: Class.Pager.php5 # Version: 1.3v.+ # Modif: 16.10.2006r. 16:30 # Description: Pager for www # ############################################################## class Pager { public $p_name; public function Pager($p_name='pager', $p_count='1', $p_per='250', $p_link='7', $p_sep=' ') { $this -> p_name = $p_name; $this -> setVar('count', $p_count); $this -> setVar('per', $p_per); $this -> setVar('html_link', '<a href="index.php?site={^link}" title="{^title}" style="text-decoration: none;">{^name}</a>n'); $this -> setVar('sep', $p_sep); $this -> setVar('link', $p_link); $this -> setVar('site', '1'); $this -> setVar('fast_link', '5'); } private function getPages() { } private function setPages($page) { $this -> setVar('site', $page); } private function buildPager() { ($this -> getVar('fast_link') >= '5' && $this -> getVar('pages') > '51' && $this -> getVar('site') > '50')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 50), 'Wstecz o 50 stron', '<strong><||</strong>'):''; ($this -> getVar('fast_link') >= '4' && $this -> getVar('pages') > '31' && $this -> getVar('site') > '30')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 30), 'Wstecz o 30 stron', '<strong><<|</strong>'):''; ($this -> getVar('fast_link') >= '3' && $this -> getVar('pages') > '11' && $this -> getVar('site') > '10')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 10), 'Wstecz o 10 stron', '<strong><|</strong>'):''; ($this -> getVar('fast_link') >= '2' && $this -> getVar('pages') > '6' && $this -> getVar('site') > '5')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 5), 'Wstecz o 5 stron', '<strong><<</strong>'):''; ($this -> getVar('fast_link') >= '1' && $this -> getVar('pages') > '1' && $this -> getVar('site') > '1')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 1), 'Wstecz o strone', '<strong><</strong>'):''; if($start <= '0') $start = '1'; if($koniec > $this -> getVar('pages')) $koniec = $this -> getVar('pages'); for($i=$start; $i <= $koniec; $i++) { ($i == $this -> getVar('site'))?$name = '<font size=+1>'.$i.'</font>':$name=$i; $this -> links[$this -> p_name][] = $this -> createLink($i, $i, $name); } $this -> links[$this -> p_name][] = 'z <strong>'.$this -> getVar('pages').'</strong>'; ($this -> getVar('fast_link') >= '1' && $this -> getVar('pages') > $this -> getVar('site'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 1), 'Naprzód o strone', '<strong>></strong>'):''; ($this -> getVar('fast_link') >= '2' && ($this -> getVar('site') + 5) <= $this -> getVar('pages'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 5), 'Naprzód o 5 stron', '<strong>>></strong>'):''; ($this -> getVar('fast_link') >= '3' && ($this -> getVar('site') + 10) <= $this -> getVar('pages'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 10), 'Naprzód o 10 stron', '<strong>|></strong>'):''; ($this -> getVar('fast_link') >= '4' && ($this -> getVar('site') + 30) <= $this -> getVar('pages'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 30), 'Naprzód o 30 stron', '<strong>|>></strong>'):''; ($this -> getVar('fast_link') >= '5' && ($this -> getVar('site') + 50) <= $this -> getVar('pages'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 50), 'Naprzód o 50 stron', '<strong>||></strong>'):''; } private function createLink($link, $title, $name) { } public function Render() { $this -> getPages(); $this -> setPages($site); $this -> buildPager(); foreach($this -> links[$this -> p_name] as $link) { $html_out .= ''.$link.''.$this -> getVar('sep').''; } $this -> p[$this -> p_name]['html_out'] = $html_out; return $html_out; } public function startRecord() { return (($this -> getVar('site') - 1) * $this -> getVar('per')); } public function getVar($get) { return $this -> p[$this -> p_name][$get]; } public function setVar($get, $var) { $this -> p[$this -> p_name][$get] = $var; } foreach($_GET as $pole => $war) { $a++; ($a==1)?$zn='?':$zn='&'; $link .= ''.$zn.''.$pole.'='.$war.''; } } return $link; } public function setFastLink($leavel = '5') { $this -> setVar('fast_link', $leavel); } } public function setHTML_Link($html_link) { $this -> setVar('html_link', $html_link); } } ?>
Klase wywolujemy :
<? include_once 'Class.Pager.php5'; // nazwa pagera || ilosc rekordow || ilosc wyswietlanych rekordow || ilosc wyswiet
lanych linkow do stron || oddzielanie linkow $p = new Pager("nazwa_pagera", $num, "25"); // jesli mamy link posiadajacy parametry i chcemy je "przepisac" do naszego nowego linku do podstrony uzywamy builder'a linkow $link = $p -> buildLink(); $link = "<a href="$link$pagess&site={^link}" title="{^title}" style="text-decoration: none;">{^name}</a>"; // funkcja ustawiajaca szablon linku $p -> setHTML_Link($link); // wywolanie - wyswietlenie pagera ?>