Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Stronnicowanie problem xdd
Forum PHP.pl > Forum > Przedszkole
raf30al
Witam ma stronnicowanie ale cos nie do konca dziala

Klasa:
  1. <?php
  2. /**
  3.  * Klasa pagera
  4.  *
  5.  * @author Krzysztof 'Balon' Jagiełło < balonyo@gmail.com >
  6.  *
  7.  * ( pager::text['separ'] - separator odzdzielajacy linki do stron )
  8.  * ( pager::url - sposob wyswietlania urli )
  9.  *
  10.  */
  11. class pager
  12. {
  13. var $ileMax = 2; # ileMax na strone
  14. var $actPage; # aktualna strona
  15. var $ilePages; # ile stron (wynik pager::ileStron)
  16. var $pagerMax = 2;
  17. var $text = array( 'PREV' => '<< Poprzednia ::', 'NEXT' => ':: Następna >>', 'T_PREV' => 'Poprzednia strona', 'T_NEXT' => 'Następna strona', 'SEPAR' => ' ', 'ZILU' => 'z % stron' );
  18. var $url = '<a href="cat.php?p=%">%</a>';
  19. var $link;
  20.  
  21. /**
  22.   * Konstruktor klasy
  23.   *
  24.   * @param integer $actPage aktualny numer strony
  25.   */
  26. function pager( $actPage, $num, $limit = 10 )
  27. {
  28. $this->actPage = $actPage;
  29. $this->ilePages = $this->ileStron( $num );
  30. $this->ileMax = $limit;
  31. }
  32. /**
  33.   * Pager
  34.   *
  35.   */
  36. function showPager()
  37. {
  38. $this->prev();
  39. $this->addSep();
  40. for( $i = ( $this->actPage / $this->ileMax ) - $this->pagerMax; $i <= ( $this->actPage / $this->ileMax ) + $this->pagerMax; $i++ )
  41. {
  42. if( $i >= 0 && $i < $this->ilePages )
  43. {
  44. $this->page( $i );
  45. $this->addSep();
  46. }
  47. }
  48. $this->addzIlu( $this->ilePages );
  49. $this->next();
  50. return $this->link;
  51. }
  52. /**
  53.   * Poprzednia strona
  54.   *
  55.   */
  56. function prev()
  57. {
  58. if( ( $this->actPage / $this->ileMax ) == 0 )
  59. {
  60. $this->link = $this->text['PREV'];
  61. }
  62. else
  63. {
  64. $this->link = $this->url( $this->actPage / $this->ileMax, '', 'minus' );
  65. }
  66. }
  67. /**
  68.   * Nastepna strona
  69.   *
  70.   */
  71. function next()
  72. {
  73. if( $this->actPage < ( ( $this->ilePages * $this->ileMax ) - $this->ileMax ) )
  74. {
  75. $this->link .= $this->url( $this->actPage / $this->ileMax, '', 'plus' );
  76. }
  77. else
  78. {
  79. $this->link .= $this->text['NEXT'];
  80. }
  81. }
  82. /**
  83.   * wyswietlanie linku do strony
  84.   *
  85.   * @param integer $i numer strony
  86.   */
  87. function page( $i )
  88. {
  89. if( $this->actPage == $i * $this->ileMax )
  90. {
  91. $this->link .= $this->url( $i , 't' );
  92. }
  93. else
  94. {
  95. $this->link .= $this->url( $i );
  96. }
  97. }
  98. /**
  99.   * Obliczanie ilosci stron
  100.   *
  101.   * @param integer $num liczba wpisow(mysql_num_rows)
  102.   * @return $ilePages
  103.   */
  104. function ileStron( $num )
  105. {
  106. return ceil( $num / $this->ileMax );
  107. }
  108.  
  109. /**
  110.   * Tworzenie urli, do $this->link podajemy juz <a href="(...)
  111.   * a miejsce na numer strony oznaczamy jako %
  112.   *
  113.   * @param integer $i
  114.   * @param string $bold
  115.   * @return $link
  116.   */
  117. function url( $i, $bold = null, $mod = null )
  118. {
  119. if( $mod == null )
  120. {
  121. if( $bold == 't' )
  122. {
  123. $link = str_replace( '>%<', '><b>'.( $i + 1 ).'</b><', $this->url );
  124. $link = str_replace( '%', $i * $this->ileMax, $link );
  125. }
  126. else
  127. {
  128. $link = str_replace( '>%<', '>'.( $i + 1 ).'<', $this->url );
  129. $link = str_replace( '%', $i * $this->ileMax, $link );
  130. }
  131. }
  132. else
  133. {
  134. if( $mod == 'plus' )
  135. {
  136. $link = str_replace( '>%<', ' title="'.$this->text['T_NEXT'].'">'.$this->
  137. text['NEXT'].'<', $this->url );
  138. $link = str_replace( '%', ( $i * $this->ileMax ) + $this->ileMax, $link );
  139. }
  140. else if( $mod == 'minus' )
  141. {
  142. $link = str_replace( '>%<', ' title="'.$this->text['T_PREV'].'">'.$this->
  143. text['PREV'].'<', $this->url );
  144. $link = str_replace( '%', ( $i * $this->ileMax ) - $this->ileMax, $link );
  145. }
  146. }
  147. return $link;
  148. }
  149. /**
  150.   * Dodawanie separatora
  151.   *
  152.   */
  153. function addSep()
  154. {
  155. $this->link .= $this->text['SEPAR'];
  156. }
  157. /**
  158.   * " (...) ze 100 "
  159.   */
  160. function addzIlu( $i )
  161. {
  162. $this->link .= str_replace( '%', $i, $this->text['ZILU'] );
  163. $this->addSep();
  164. }
  165. }
  166. ?>


Kod:

  1. include("klasa.php");
  2. if( isset( $_GET['p'] ) && is_numeric( $_GET['p'] ) )
  3. {
  4. $p = $_GET['p'];
  5. }
  6. else
  7. {
  8. $p = 0;
  9. }
  10.  
  11.  
  12. $result = mysql_query("SELECT * FROM articles WHERE id_cat='$id' ORDER BY tytul ASC");
  13. $num_rows = mysql_num_rows($result);
  14. // i je wyświetlamy
  15. echo '';
  16.  
  17. while($row = mysql_fetch_array($result))
  18. {echo '<tr id="s">';
  19. echo '<td><a href="art.php?id='.$row['id'].'"><b>'.stripslashes($row['tytul']).'</b></a></td><td align=right> <b>'.stripslashes($row['opis']).'</b></td>';
  20.  
  21. }
  22. echo '</tr>';
  23.  
  24. $pager = new pager( $p, $num_rows );
  25.  
  26. echo $pager->showPager();


I dziala ona na zasadzie ze: Mam 3 rekordy w bazie a chce zeby wyswietlala aby 2 i nastepna strona a skrypt robi:

Cytat
<< Poprzednia :: 1 2 z 2 stron :: Nastêpna >>

i niby dobrze ale na kazdej stronie wyswietla wszystkie rekordy z bazy. Co źle zrobiłem?
pedro84
Kod
#
$result = mysql_query("SELECT * FROM articles WHERE id_cat='$id' ORDER BY tytul ASC");

Pobierasz wszystkie to wyświetla wszystkie. Poszukaj, LIMIT oraz OFFSET.
nospor
1) zakladasz znowu temat na to samo. To sie nazywa crossposting i jest zabronione. Jak pojawily sie nowe fakty to trzeba bylo napisac do moderatora by otworzyl stary temat
2) mimo iz ci mowiono o LIMIT ty nadal tego nie uzywasz
1+2 =3

3) zamykam
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.