Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Pager ( stronicowanie )
Adrian Staniszew...
post
Post #1





Grupa: Zarejestrowani
Postów: 94
Pomógł: 0
Dołączył: 17.10.2006
Skąd: Łódź

Ostrzeżenie: (0%)
-----


Witam,

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 :
  1. <?
  2. ###############################################################
  3. # Adrian Staniszewski
  4. # adrian_s@toya.net.pl
  5. # GG: 5513236
  6. #
  7. # File: Class.Pager.php5
  8. # Version:  1.3v.+
  9. # Modif:  16.10.2006r. 16:30
  10. # Description:  Pager for www
  11. #
  12. ##############################################################
  13.  
  14. class Pager {
  15.  
  16. public $p_name;
  17. private $p = array();
  18. private $links = array();
  19.  
  20. public function Pager($p_name='pager', $p_count='1', $p_per='250', $p_link='7', $p_sep=' ') {
  21. $this -> p_name = $p_name;
  22. $this -> setVar('count', $p_count);
  23. $this -> setVar('per', $p_per);
  24. $this -> setVar('html_link', '<a href="index.php?site={^link}" title="{^title}" style="text-decoration: none;">{^name}</a>n');
  25. $this -> setVar('sep', $p_sep);
  26. $this -> setVar('link', $p_link);
  27. $this -> setVar('site', '1');
  28. $this -> setVar('fast_link', '5');
  29. }
  30.  
  31. private function getPages() {
  32. $this -> setVar('pages', ceil(($this -> getVar('count')/$this -> getVar('per'))));
  33. }
  34.  
  35. private function setPages($page) {
  36. $this -> setVar('site', $page);
  37. }
  38.  
  39. private function buildPager() {
  40.  
  41. ($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>'):'';
  42. ($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>'):'';
  43. ($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>'):'';
  44. ($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>'):'';
  45. ($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>'):'';
  46.  
  47. $start = ($this -> getVar('site') - ceil($this -> getVar('link')/2));
  48. if($start <= '0') $start = '1';
  49.  
  50. $koniec = ($this -> getVar('site') + ceil($this -> getVar('link')/2));
  51. if($koniec > $this -> getVar('pages')) $koniec = $this -> getVar('pages');
  52.  
  53. for($i=$start; $i <= $koniec; $i++) {
  54.  ($i == $this -> getVar('site'))?$name = '<font size=+1>'.$i.'</font>':$name=$i;
  55.  $this -> links[$this -> p_name][] = $this -> createLink($i, $i, $name);
  56. }
  57. $this -> links[$this -> p_name][] = 'z <strong>'.$this -> getVar('pages').'</strong>';
  58. ($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>'):'';
  59. ($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>'):'';
  60. ($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>'):'';
  61. ($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>'):'';
  62. ($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>'):'';
  63. }
  64.  
  65. private function createLink($link, $title, $name) {
  66. return preg_replace('/{^(.*?)}/sie', '$$1', $this -> getVar('html_link'));
  67. }
  68.  
  69. public function Render() {
  70. $this -> getPages();
  71. $site = ($_GET['site']<>'' and is_numeric($_GET['site']))?''.$_GET['site'].'':'1';
  72. $this -> setPages($site);
  73. $this -> buildPager();
  74.  
  75. foreach($this -> links[$this -> p_name] as $link) {
  76. $html_out .= ''.$link.''.$this -> getVar('sep').'';
  77. }
  78. $this -> p[$this -> p_name]['html_out'] = $html_out;
  79. return $html_out;
  80. }
  81.  
  82. public function startRecord() {
  83. return (($this -> getVar('site') - 1) * $this -> getVar('per'));
  84. }
  85.  
  86. public function getVar($get) {
  87. return $this -> p[$this -> p_name][$get];
  88. }
  89.  
  90. public function setVar($get, $var) {
  91. $this -> p[$this -> p_name][$get] = $var;
  92. }
  93.  
  94. public function buildLink($link = 'index.php5', $delete = array('site')) {
  95.  
  96. foreach($_GET as $pole => $war) {
  97. $pole = trim($pole);
  98. if(!in_array($pole, $delete)) {
  99. $a++;
  100. ($a==1)?$zn='?':$zn='&';
  101. $link .= ''.$zn.''.$pole.'='.$war.'';
  102. }
  103. }
  104. return $link;
  105. }
  106.  
  107. public function setFastLink($leavel = '5') {
  108. if(is_numeric($leavel) && $leavel >= '0' && $leavel <= '5') {
  109. $this -> setVar('fast_link', $leavel);
  110. }
  111. }
  112.  
  113. public function setHTML_Link($html_link) {
  114. $this -> setVar('html_link', $html_link);
  115. }
  116.  
  117. }
  118. ?>


Klase wywolujemy :
  1. <?
  2. include_once 'Class.Pager.php5';
  3.  
  4. // nazwa pagera || ilosc rekordow || ilosc wyswietlanych rekordow || ilosc wyswiet
    lanych linkow do stron || oddzielanie linkow
  5. $p = new Pager("nazwa_pagera", $num, "25");
  6. // jesli mamy link posiadajacy parametry i chcemy je "przepisac" do naszego nowego linku do podstrony uzywamy builder'a linkow
  7. $link = $p -> buildLink();
  8. $link = "<a href="$link$pagess&site={^link}" title="{^title}" style="text-decoration: none;">{^name}</a>";
  9. // funkcja ustawiajaca szablon linku
  10. $p -> setHTML_Link($link);
  11. // wywolanie - wyswietlenie pagera
  12. echo $p -> Render();
  13.  
  14. ?>


Ten post edytował Adrian Staniszewski 21.10.2006, 12:07:00
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Adrian Staniszew...
post
Post #2





Grupa: Zarejestrowani
Postów: 94
Pomógł: 0
Dołączył: 17.10.2006
Skąd: Łódź

Ostrzeżenie: (0%)
-----


Wydanie wersji 1.3 (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Jest kilka istotnych zmian :
- ograniczyłem ilosc parametrow ustawianych w wywoalniu klasy (funkcja Pager)
- dodalem funkcje setFastLink(); - (opis w pierwszym poscie)
- poprawilem i zmienilem funkcje buildLink(); (usunalem GLOBAL)
- dodalem funkcje setHTML_Link(); zmieniajaca szablon linku HTML (mozna tutaj uzywac pomocniczo buildLink(); )
- dodałem ograniczenie wyswietlania "szybkich linkow"

Zapraszam do testowania i tworzenia projektow w oparciu o moj prosty Pager (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

p.s
przeprowadzilem badanie wydajnosci Pagera
wyniki sa oparte na 50 tys. wywolan, 100 tys. rekordow, 25 na strone

sredni czas generowania : 0,0025s (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)


jezeli chcemy w kilku miejscach na stronie wyswietlic nasz raz wygenerowany pager uzywamy :

  1. <?
  2.  
  3. echo $p -> getVar("html_out");
  4.  
  5. ?>


gdy na stronie mamy kilka Pagerow - rozrozniamy je po nazwie zadeklarowanej w wywolaniu klasy - wystarczy zmienic nazwe aktualnego Pagera poprzez:
  1. <?
  2.  
  3. $p -> p_name = "nazwa_obecnego_pagera";
  4.  
  5. ?>


dodatkowo gdy pobieramy dane z bazy, skrypt latwo i szybko wygeneruje nam parametry pobierania:

  1. <?
  2.  
  3. $start = $p -> startRecord();
  4. $koniec = $p -> getVar("per");
  5.  
  6. ?>


Wszystkim szukajacym profesjonanego Pagera zapraszam do klasy nospora a lubiacym testowanie zapraszam do testowania (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Pozdrawiam
Adrian Staniszewski

edit..

Funkcja buildLink();
wywolujemy ja po prostu :

  1. <?
  2.  
  3. // w funkcji mozemy a raczej powinnismy zadeklarowac nazwe zmiennej GET w ktorej b
    edziemy trzymac numer strony, aby skrypt usunal ja z tworzonego linku - mozemy tez podac inne zmienne ktore chcemy usunac z linku - calosc podajemy w formie tablicy
  4. $link = $p -> buildLink();
  5.  
  6. // funkcja ta posiada 2 parametry - nazwa pliku (ew. sciezka do pliku) oraz wykluczenia (co ma zostac usuniete z linku)
  7.  
  8. $delete = array('site', 'kat', 'parent'); // wykluczenia
  9. $link = $p -> buildLink('index.php', $delete);
  10.  
  11. ?>


w najnowszej wersji dodano tez funkcje setFastLink(); ustawia ona ile szybkich linkow ma zostac wygenerowanych w danym Pagerze - domyslnie jest to 5 na "strone" (+1, +5, +10, +30, +50, -1, -5, -10, -30, -50)

wartosc tego parametru to 0-5, a uzywamy go :

  1. <?
  2.  
  3. // przed wywolaniem funkcji Render();
  4. $p -> setFastLink('3');
  5.  
  6. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.12.2025 - 09:20