Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Wyświetlanie newsów
Loped
post
Post #1





Grupa: Zarejestrowani
Postów: 120
Pomógł: 0
Dołączył: 9.04.2009

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


Witam. Zrobiłem właśnie panel newsów. W indexie wyświetlam je wszystkie za pomocą pętli while. Bym chciał zrobić coś takiego, że wyświetla 10 najnowszych newsów, a niżej pojawia się np. "strona 1 z 2". Każdy news ma swój id.

O to kod ich wyświetlania:

  1. <?php
  2. $sql="SELECT newsy.id, DATE_FORMAT(newsy.dodano, '%e-%m-%Y %H:%i') AS utworzono,
  3.        newsy.autor, newsy.temat, newsy.tresc, newsy.userid, COUNT(lp) AS il_kom
  4.        FROM newsy
  5.        LEFT JOIN komentarze ON (komentarze.id=newsy.id)
  6.        GROUP BY newsy.id ORDER BY newsy.dodano DESC";
  7.  $query = $lacz->query($sql);
  8.  while($result = $query->fetch_assoc())
  9.  {
  10.      $result['tresc'] = strip_tags($result['tresc']);    
  11.      $result['tresc'] = BBCode($result['tresc']);
  12. echo '<center><b><div id="TYTUL"><br>'.nl2br($result['temat']).'</div></center></b>';
  13. echo '<p>'.nl2br($result['tresc']).'</p>';
  14.    echo '<p><b>Autor:</b><font color="#89b9a6"> <i><a href="profile.php?userid='.$result['userid'].'">'.$result['autor'].'</a> ('.$result['utworzono'].')</font></i></p>';
  15.    echo '<p><a href="index.php?newsid='.$result['id'].'">Komentarze: ('.$result['il_kom'].')</a></p>';
  16.  
  17.  }
  18. }
  19. ?>
Go to the top of the page
+Quote Post
Spawnm
post
Post #2





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




w sql LIMIT i poszukaj w google/ na forum : stronicowanie / pager , jest tego pełno .
Go to the top of the page
+Quote Post
Loped
post
Post #3





Grupa: Zarejestrowani
Postów: 120
Pomógł: 0
Dołączył: 9.04.2009

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


Mam taki kod:

  1. <?php
  2. $start = $_GET['start'];
  3. $na_stronie = 2;
  4.  
  5. $wykonaj=$lacz->query("SELECT * FROM newsy");
  6. $znaleziono=$wykonaj->num_rows;
  7. if($znaleziono>$na_stronie) {
  8.    print '<center>Strona ';
  9. for($i=0; $i<ceil($znaleziono/$na_stronie); $i++)
  10.  print '<a href="newmsg.php?start='.($i*$na_stronie).'">'.($i+1).'</a> | ';
  11. }  
  12. print '</center>';
  13.  
  14.  $query="SELECT newsy.id, DATE_FORMAT(newsy.dodano, '%e-%m-%Y %H:%i') AS utworzono,
  15.        newsy.autor, newsy.temat, newsy.tresc, newsy.userid, COUNT(lp) AS il_kom
  16.        FROM newsy
  17.        LEFT JOIN komentarze ON (komentarze.id=newsy.id)
  18.        GROUP BY newsy.id ORDER BY newsy.dodano DESC LIMIT ".($start).",".$na_stronie."";
  19. $q = $lacz->query ($query);
  20.        while ($result = $q->fetch_assoc())
  21.        {
  22.  
  23.      $result['tresc'] = strip_tags($result['tresc']);    
  24.      $result['tresc'] = BBCode($result['tresc']);
  25. echo '<center><b><div id="TYTUL"><br>'.nl2br($result['temat']).'</div></center></b>';
  26. echo '<p>'.nl2br($result['tresc']).'</p>';
  27.    echo '<p><b>Autor:</b><font color="#89b9a6"> <i><a href="profile.php?userid='.$result['userid'].'">'.$result['autor'].'</a> ('.$result['utworzono'].')</font></i></p>';
  28.    echo '<p><a href="index.php?newsid='.$result['id'].'">Komentarze: ('.$result['il_kom'].')</a></p>';
  29.  
  30. }
  31. ?>


I aby wejść w newsy musze wpisać index.php?start=0. jak wpisze index.php to się nic nie pojawia... Jak mogę zrobić, ze jak wpiszę index.php pojawi mi się strona tak jak w index.php?start=0?
Go to the top of the page
+Quote Post
Spawnm
post
Post #4





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




  1. <?php
  2. $start = ( $_GET['start'] )? $_GET['start']: 0;
  3. ?>
Go to the top of the page
+Quote Post
Loped
post
Post #5





Grupa: Zarejestrowani
Postów: 120
Pomógł: 0
Dołączył: 9.04.2009

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


Dzięki. Punkt dla Ciebie ;P Jeszcze chiałbym zrobić aby podświetlało mi liczby stron na której aktualnie jestem. Np. 1|2|3|4
Go to the top of the page
+Quote Post
Spawnm
post
Post #6





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




  1. <?php
  2. for($i=0; $i<ceil($znaleziono/$na_stronie); $i++){
  3.  if($i==$_GET['start']){
  4.    echo '<b><a href="newmsg.php?start='.$i.'">'.($i+1).'</a></b> | ';
  5.  }else{
  6.    echo '<a href="newmsg.php?start='.$i.'">'.($i+1).'</a> | ';
  7.  }
  8. }
  9. ?>


a $i*$na_stronie dawaj tylko raz na gecie
Go to the top of the page
+Quote Post
Loped
post
Post #7





Grupa: Zarejestrowani
Postów: 120
Pomógł: 0
Dołączył: 9.04.2009

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


Czyli gdzie te $i*$na_stronie mam dać? bo trochę tego nie rozumiem ;P
Go to the top of the page
+Quote Post
Spawnm
post
Post #8





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




  1. <?php
  2. $start = ( $_GET['start'] )? $_GET['start']: 0;
  3. $na_stronie = 2;
  4. $start=$start*$na_stronie;
  5. ?>
Go to the top of the page
+Quote Post

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.08.2025 - 00:20