Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [skrypt] Paginator
Forum PHP.pl > Inne > Oceny
CzesLaW'ek
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. ?>
Fipaj
Hmmm... Normalne smile.gif

Tylko czemu dajesz do oceny nie testowany skrypt??
CzesLaW'ek
Testowany... tylko nie w 100% smile.gif Np. może się coś jeszcze walić przy tworzeniu linków do stron...
czachor
No to zapodaj testowany w 100%, to będzie co oceniać smile.gif
CzesLaW'ek
Powinno być już OK z tymi linkami smile.gif
CzesLaW'ek
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.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.