Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [skrypt] Paginator, klasa do stronnicowania :)
CzesLaW'ek
post 20.05.2005, 15:02:30
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 7.12.2004

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


Kolejna klasa w moim wykonaniu. Proszę o ocenę oraz sugestie... wszelkie komentarze bardzo mile widziane. Jako, że klasy nie testowałem jeszcze dogłębnie nie jest pewnie wolna od błędów.

Przeznaczenie
  • Porcjowanie wyników na stronie
  • Wyświetlanie nawigatora w dwóch różnych formatach
  1. <?php
  2.  
  3. ########################################
  4. ##
  5. ## myPaginator v0.9 b
  6. ## Written by CzesLaW
  7. ##
  8. ## E-Mail: czesio@e-clubbing.net
  9. ## Website: http://e-clubbing.net
  10. ##
  11. ########################################
  12. ##
  13. ## Jeśli masz jakieś sugestie odnośnie skryptu,
  14. ## napisz do mnie e-mail'a.
  15. ##
  16. ########################################
  17.  
  18. class myPaginator
  19. {
  20. public $navigator = '';
  21. public $url = '';
  22. public $count_from = 0;
  23. public $per_page = 0;
  24. public $count_all = 0;
  25.  
  26. public function myPaginator( $per_page = 5 )
  27. {
  28. $this -> count_from = ( !empty( $_GET['p'] ) ? $_GET['p'] : 0 );
  29.  
  30. $this -> per_page = $per_page;
  31.  
  32. return true;
  33. }
  34.  
  35. public function showNavigator( $count_all = 0, $type = 0 , $long = 1)
  36. {
  37. if( $count_all !== 0 )
  38. {
  39. $this -> count_all = $count_all;
  40.  
  41. $this -> getUrl();
  42.  
  43. if( $this -> count_from > 0 )
  44. {
  45. $this -> navigator .= '<a href=\"./'. $this -> url . ( ( !empty( $this -> url ) ) ? '&amp;' : '?' ) .'p='. ( $this -> count_from - $this -> per_page ) .'\">&lt;&lt;';
  46. $this -> navigator .= ( $long == 1 ) ? '&nbsp;<strong>Poprzednie</strong>' : '';
  47. $this -> navigator .= '</a> ';
  48. }
  49.  
  50. $this -> navigator .= ( $type == 0 ) ? $this -> pageNavigator() : $this -> counterNavigator();
  51.  
  52. if( $this -> count_all > ( $this -> count_from + $this -> per_page ) )
  53. {
  54. $this -> navigator .= '<a href=\"./'. $this -> url . ( ( !empty( $this -> url ) ) ? '&amp;' : '?' ) .'p='. ( $this -> count_from + $this -> per_page ) .'\">';
  55. $this -> navigator .= ( $long == 1 ) ? '<strong>Następne</strong>&nbsp;' : '';
  56. $this -> navigator .= '&gt;&gt;</a>';
  57. }
  58.  
  59. return $this -> navigator;
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. }
  66.  
  67. private function getUrl()
  68. {
  69. $this -> url = substr( str_replace( '&', '&amp;', preg_replace( '!(.*?)[&|?]p=.*!si', '$1', $_SERVER['REQUEST_URI'] ) ), 1 );
  70.  
  71. $this -> url = ( $this -> url == '?' ) ? '' : $this -> url;
  72.  
  73. return true;
  74. }
  75.  
  76. private function pageNavigator()
  77. {
  78. $pages = ceil( $this -> count_all / $this -> per_page );
  79.  
  80. if( $pages > 1 )
  81. {
  82. $pageNavigator = '';
  83.  
  84. for( $x = 0; $x < $pages; $x++ )
  85. {
  86. if( $x < 3 || $x >= ( $pages - 3 ) || ( $x >= ( ( $this -> count_from / $this -> per_page ) - 1 ) && $x <= ( ( $this -> count_from / $this -> per_page ) + 1 ) ) )
  87. {
  88. if( $x == ( $this -> count_from / $this -> per_page ) )
  89. {
  90. $pageNavigator .= '[<strong>'. ( $x + 1 ) .'</strong>] ';
  91. }
  92. else
  93. {
  94. $pageNavigator .= '<a href=\"./'. $this -> url . ( ( !empty( $this -> url ) ) ? '&amp;' : '?' ) .'p='. ( $x * $this -> per_page ) .'\">'. ( $x + 1) .'</a> ';
  95. }
  96. }
  97. else
  98. {
  99. $pageNavigator .= '*';
  100. }
  101. }
  102.  
  103. $pageNavigator = ereg_replace( '[*]+', '...', $pageNavigator );
  104.  
  105. return $pageNavigator;
  106. }
  107. else
  108. {
  109. return false;
  110. }
  111. }
  112.  
  113. private function counterNavigator()
  114. {
  115. $pages = ceil( $this -> count_all / $this -> per_page );
  116.  
  117. if( $pages > 1 )
  118. {
  119. $pageNavigator = '<strong>'. ( ( $this -> count_from / $this -> per_page ) + 1 ) .'</strong>&nbsp;';
  120. $pageNavigator .= '/';
  121. $pageNavigator .= '&nbsp;<strong>'. $pages .'</strong>&nbsp;';
  122.  
  123. return $pageNavigator;
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. }
  131.  
  132. ?>


Przykład zastosowania:

- Dzielenie wyników przy mySQL'u:

  1. <?php
  2.  
  3. require_once ( './myPaginator.class.php' );
  4. $nav = new myPaginator( 8 ); // w nawiasie podajemy ile rekordów na stronę
  5.  
  6. $sql = mysql_query( 'SELECT * FROM tabela LIMIT '. $nav -> count_from .','. $nav -> per_page );
  7.  
  8. while( $tab = mysql_fetch_array( $sql ) )
  9. {
  10.  
  11. // itd...
  12.  
  13. }
  14.  
  15. $sql = mysql_query( 'SELECT id FROM tabela' );
  16.  
  17. echo $nav -> showNavigator( mysql_num_rows( $sql ), 1, 0 ); // w nawiasie po kolei: liczba wszystkich rekordów, tryb stron / licznika, format skrócony / pełny
  18.  
  19.  
  20. ?>


Ten post edytował CzesLaW'ek 29.05.2005, 22:42:40
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 5)
Fipaj
post 20.05.2005, 15:13:43
Post #2





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

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


Hmmm... Normalne smile.gif

Tylko czemu dajesz do oceny nie testowany skrypt??


--------------------
Go to the top of the page
+Quote Post
CzesLaW'ek
post 20.05.2005, 15:15:56
Post #3





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 7.12.2004

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


Testowany... tylko nie w 100% smile.gif Np. może się coś jeszcze walić przy tworzeniu linków do stron...
Go to the top of the page
+Quote Post
czachor
post 20.05.2005, 16:39:08
Post #4





Grupa: Zarejestrowani
Postów: 897
Pomógł: 40
Dołączył: 16.12.2003
Skąd: Warszawa

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


No to zapodaj testowany w 100%, to będzie co oceniać smile.gif


--------------------
how many SEO experts does it take to change a light bulb,lightbulb,light,bulb,lamp,lighting,switch,sex,xxx
5-Reasons-why-you-should-NEVER-fix-a-computer-for-free
Go to the top of the page
+Quote Post
CzesLaW'ek
post 29.05.2005, 22:43:18
Post #5





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 7.12.2004

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


Powinno być już OK z tymi linkami smile.gif
Go to the top of the page
+Quote Post
CzesLaW'ek
post 30.05.2005, 22:04:40
Post #6





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 7.12.2004

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


Cytat
Dla mnie wywałanie takiej klasy powinno się mieścić w 1 linijce


Jeśli wywoła się tą klasę od razu w miejscu wyświetlania nawigatora (tzn. w jednej linijce) nie będzie co wstawić w "LIMIT" do mysql_query :/ chyba, że masz na to jakieś rozwiązanie smile.gif

Cytat
lepszym rozwiązaniem byłoby przypisanie HTMLa zmiennym


Moim zdaniem trochę bez sensu tworzyć tyle zmiennych, każda z małym kawałkiem kodu HTML...

Cytat
Po czwarte nie filtrujesz danych pochodzących z tablicy GET


Zgadzam się w 100%, to jest do poprawki...

Dziękuję bardzo z rady, każda jest dla mnie pomocna.
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 13.08.2025 - 22:46