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
NetJaro
post
Post #2





Grupa: Zarejestrowani
Postów: 475
Pomógł: 0
Dołączył: 1.04.2005
Skąd: Warszawa

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


Większych zastrzeżeń nie mam, ale tylko mała uwaga - z racji, że stronicowanie jest czasami dość ciężką "operacją" dla Apache - starałbym się jak najbardziej optymalizować skrypt który za to odpowiada - i dlatego wszystkie " " (cudzysłowia) zamieniłbym na ' ' (pojedyńcze apostrofy/cudzysłowia). Większa to szybkość wykonania kodu. Dodatkowo przy liczbach:

Cytat
$p = new Pager("nazwa_pagera", $num, "25");

Nie trzeba używać żadnych cudzysłowiów ;-)

I widzę stary nawyk z PHP4 - aby stworzyć konstruktor wystarczy nazwać funkcję __construct, a nie jak w PHP4 - tak jak nazwa klasy. Skoro klasa i tak nie pójdzie na PHP4 to po co używać jego części ;-)

Ten post edytował NetJaro 17.10.2006, 21:16:59
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: 5.10.2025 - 11:08