Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP + MySQL] Sortowanie danych pobranych z bazy danych
henio
post
Post #1





Grupa: Zarejestrowani
Postów: 237
Pomógł: 1
Dołączył: 15.10.2004
Skąd: Lublin

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


Mam pewien kłopot z posortowaniem. Otóż mam tabele forum_topic i forum_post i chce posortowac wszystkie tematy malejąco po ostatnim dodanym poscie w danym temacie.
Fragment kodu:
  1. <?php
  2. elseif(@$_GET['id_forum_watek'] && @$_GET['id_forum_dzial'])
  3. {
  4. naglowekforum("Zobacz forum numer ".@$_GET['id_forum_dzial']."");
  5. echo "<a href='index.php?id_forum_dzial=".@$_GET['id_forum_dzial']."&id_forum_watek=".@$_GET['id_forum_watek']."&czynnosc=utworz_topic'>Utwórz nowy temat</a>\n";
  6. echo "<table align='center' width='90%' border='1' style='border-collapse: collapse'><tr bgcolor='#CCCFF'>";
  7. echo "<th colspan='2'>Ważne tematy</th><th width='50'>Posty</th><th width='100'>Autor</th><th width='150'>Ostatni post</th>\n";
  8. echo "</table><br />\n";
  9. echo "<table align='center' width='90%' border='1' style='border-collapse: collapse'><tr bgcolor='#CCCFF'>";
  10. echo "<th colspan='2'>Tematy</th><th width='50'>Posty</th><th width='100'>Autor</th><th width='150'>Ostatni post</th>\n";
  11.  
  12. $query4 = "SELECT * FROM forum_topic WHERE id_forum_dzial = '".@$_GET['id_forum_dzial']."' AND id_forum_watek='".@$_GET['id_forum_watek']."'";
  13. $result4 = mysqli_query($mysql, $query4) or die('zapytanie: '.$query4.'---blad:'.mysqli_error($mysql));
  14.  
  15. while ($row4 = mysqli_fetch_assoc($result4))
  16. {
  17. echo "<tr><td>=></td><td width='60%'><a href='index.php?id_forum_dzial=".@$_GET['id_forum_dzial']."&id_forum_watek=".@$_GET['id_forum_watek']."&id_forum_topic=".$row4['id_forum_topic']."'>".$row4['tytul']."</a><br />".$row4['podtytul']."</td><td>";
  18.  
  19. $query8 = "SELECT * FROM forum_post WHERE id_forum_topic = '".$row4['id_forum_topic']."'";
  20. $result8 = mysqli_query($mysql, $query8) or die('zapytanie: '.$query8.'---blad:'.mysqli_error($mysql));
  21. $liczba_postow = mysqli_num_rows($result8);
  22. echo "".$liczba_postow."</td><td>";
  23.  
  24. $query5 = "SELECT nick FROM forum_uzytkownicy WHERE id_forum_user = '".$row4['id_forum_user']."'";
  25. $result5 = mysqli_query($mysql, $query5) or die('zapytanie: '.$query5.'---blad:'.mysqli_error($mysql));
  26. while ($row5 = mysqli_fetch_assoc($result5))
  27. {
  28. echo "".$row5['nick']."</td><td>";
  29. }
  30.  
  31. $query6 = "SELECT data_powstania, id_forum_user FROM forum_post WHERE id_forum_topic =
     '"
    .$row4['id_forum_topic']."' ORDER BY data_powstania DESC LIMIT 0,1";
  32. $result6 = mysqli_query($mysql, $query6) or die('zapytanie: '.$query6.'---blad:'.mysqli_error($mysql));
  33. while ($row6 = mysqli_fetch_assoc($result6))
  34. {
  35. echo "".$row6['data_powstania']."<br />";
  36. $query7 = "SELECT nick FROM forum_uzytkownicy WHERE id_forum_user = '".$row6['id_forum_user']."'";
  37. $result7 = mysqli_query($mysql, $query7) or die('zapytanie: '.$query7.'---blad:'.mysqli_error($mysql));
  38. while ($row7 = mysqli_fetch_assoc($result7))
  39. {
  40. echo "".$row7['nick']."</td>";
  41. }
  42. }
  43. echo "</tr>\n";
  44. }
  45. echo "</table>\n";
  46. }
  47. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
henio
post
Post #2





Grupa: Zarejestrowani
Postów: 237
Pomógł: 1
Dołączył: 15.10.2004
Skąd: Lublin

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


To już poprawiłem. Tylko że obecnie wyświetla mi te tematy tyle razy ile jest postów.

Np:
=> Koszulki 3 Henio Dzisiaj o 14:57 Heniek
=> Koszulki 3 Henio Dzisiaj o 14:57 Heniek
fragment kodu:
  1. <?php
  2. elseif(@$_GET['id_forum_watek'] && @$_GET['id_forum_dzial'])
  3. {
  4. naglowekforum("Zobacz forum numer ".@$_GET['id_forum_dzial']."");
  5. echo "<a href='index.php?id_forum_dzial=".@$_GET['id_forum_dzial']."&id_forum_watek=".@$_GET['id_forum_watek']."&czynnosc=utworz_topic'>Utwórz nowy temat</a>\n";
  6. echo "<table align='center' width='90%' border='1' style='border-collapse: collapse'><tr bgcolor='#CCCFF'>";
  7. echo "<th colspan='2'>Ważne tematy</th><th width='50'>Posty</th><th width='100'>Autor</th><th width='150'>Ostatni post</th>\n";
  8. echo "</table><br />\n";
  9. echo "<table align='center' width='90%' border='1' style='border-collapse: collapse'><tr bgcolor='#CCCFF'>";
  10. echo "<th colspan='2'>Tematy</th><th width='50'>Posty</th><th width='100'>Autor</th><th width='150'>Ostatni post</th>\n";
  11.  
  12. $query4 = "SELECT t.id_forum_topic, t.tytul, t.podtytul, t.id_forum_user, max(p.data_powstania) 
  13.  FROM forum_topic AS t 
  14.  JOIN forum_post AS p ON (p.id_forum_topic = t.id_forum_topic) 
  15.  WHERE t.id_forum_dzial = '".@$_GET['id_forum_dzial']."' AND t.id_forum_watek='".@$_GET['id_forum_watek']."' 
  16.  GROUP BY p.id_forum_post 
  17.  ORDER BY max(p.data_powstania) DESC";
  18. $result4 = mysqli_query($mysql, $query4) or die('zapytanie: '.$query4.'---blad:'.mysqli_error($mysql));
  19.  
  20. while ($row4 = mysqli_fetch_assoc($result4))
  21. {
  22. echo "<tr><td>=></td><td width='60%'><a href='index.php?id_forum_dzial=".@$_GET['id_forum_dzial']."&id_forum_watek=".@$_GET['id_forum_watek']."&id_forum_topic=".$row4['id_forum_topic']."'>".$row4['tytul']."</a><br />".$row4['podtytul']."</td><td>";
  23.  
  24. $query8 = "SELECT * FROM forum_post WHERE id_forum_topic = '".$row4['id_forum_topic']."'";
  25. $result8 = mysqli_query($mysql, $query8) or die('zapytanie: '.$query8.'---blad:'.mysqli_error($mysql));
  26. $liczba_postow = mysqli_num_rows($result8);
  27. echo "".$liczba_postow."</td><td>";
  28.  
  29. $query5 = "SELECT nick FROM forum_uzytkownicy WHERE id_forum_user = '".$row4['id_forum_user']."'";
  30. $result5 = mysqli_query($mysql, $query5) or die('zapytanie: '.$query5.'---blad:'.mysqli_error($mysql));
  31. while ($row5 = mysqli_fetch_assoc($result5))
  32. {
  33. echo "".$row5['nick']."</td><td>";
  34. }
  35.  
  36. $query6 = "SELECT data_powstania, id_forum_user FROM forum_post WHERE id_forum_topic =
     '"
    .$row4['id_forum_topic']."' ORDER BY data_powstania DESC LIMIT 0,1";
  37. $result6 = mysqli_query($mysql, $query6) or die('zapytanie: '.$query6.'---blad:'.mysqli_error($mysql));
  38. while ($row6 = mysqli_fetch_assoc($result6))
  39. {
  40. $czasa = explode(', ', $row6['data_powstania']);
  41. $czasday = $czasa['0'];
  42. $czastime = $czasa['1'];
  43. $czasa1 = explode('-', $czasa['0']);
  44. $czasa2 = explode(':', $czasa['1']);
  45. $czasunixhour = mktime($czasa2['0'], $czasa2['1']);
  46. if($czasday < date('Y-m-d'))
  47. {
  48. echo "".$row6['data_powstania']."<br />";
  49. }
  50. elseif($czasday = date('Y-m-d'))
  51. {
  52. $roznicza = time() - $czasunixhour;
  53. $roznicza = floor($roznicza / 60);
  54. if($roznicza < 60)
  55. {
  56. echo "".$roznicza." minut temu<br />";
  57. }
  58. else
  59. {
  60. echo "Dzisiaj o ".$czastime."<br />";
  61. }
  62. }
  63. $query7 = "SELECT nick FROM forum_uzytkownicy WHERE id_forum_user = '".$row6['id_forum_user']."'";
  64. $result7 = mysqli_query($mysql, $query7) or die('zapytanie: '.$query7.'---blad:'.mysqli_error($mysql));
  65. while ($row7 = mysqli_fetch_assoc($result7))
  66. {
  67. echo "".$row7['nick']."</td>";
  68. }
  69. }
  70. echo "</tr>\n";
  71. }
  72. echo "</table>\n";
  73. }
  74. ?>
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: 11.10.2025 - 03:26