Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php/mysql] Nietypowy problem z pętlą while
rafnal
post 13.07.2010, 13:13:57
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 3.11.2007

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


Witam,

A oto moj nietypowy problem z jakim sie borykam a
mianowicie potrzebuje pobrac jedna petla dane z tej samej tabeli
ale niestety musza byc zastosowane 2 petle
aby wyswietlic poprawnie to co potrzebuje.
Kod wyglada na chwile obecna tak:

petla 1

  1. while (($row = mysql_fetch_array($res) != NULL) {
  2. ?>
  3. <tr><td><p><?for($i=0;$i<10;$i++)echo $row[date][$i];?></p></td><td
  4. align="center"><p><?=$row['ip_count']?></p></td><td
  5. align="center"><p><?=$row2['ip_count']?></p></td></tr>
  6. <?
  7. }


petla 2

  1. while (($row2 = mysql_fetch_array($res2) != NULL) {
  2. ?>
  3. <tr><td><p><?for($i=0;$i<10;$i++)echo $row2[date][$i];?></p></td><td
  4. align="center"><p><?=$row['ip_count']?></p></td><td
  5. align="center"><p><?=$row2['ip_count']?></p></td></tr>
  6. <?
  7. }



Chce aby jedna petla wyswietlala dane. Probowalem tez zrobic petle w petli to
pobiera tylko dane z jednego zapytania a wynik drugiego wyswietla czesciowo.

  1. <?
  2. $res = mysql_query("SELECT COUNT(DISTINCT ip) as ip_count FROM stats
  3. WHERE site = $site AND date >= '" . date("Ymd") . "' - $period + 1");
  4. $row = mysql_fetch_array($res);
  5. $total_count = $row[ip_count];
  6.  
  7. $res2 = mysql_query("SELECT COUNT(ip) as ip_count FROM stats WHERE
  8. site = $site AND date >= '" . date("Ymd") . "' - $period + 1");
  9. $row2 = mysql_fetch_array($res2);
  10. $total_count2 = $row2[ip_count];
  11.  
  12. $res3 = mysql_query("SELECT date, COUNT(DISTINCT ip) as ip_count FROM
  13. stats WHERE site = $site AND date >= '" . date("Ymd") . "' - $period +
  14. 1 GROUP BY date ORDER BY date DESC");
  15.  
  16. $res4 = mysql_query("SELECT date, COUNT(ip) as ip_count FROM stats
  17. WHERE site = $site GROUP BY date ORDER BY date DESC");
  18.  
  19. $max = 0;
  20. while (($row = mysql_fetch_array($res3)) != NULL) {
  21. if ($row[ip_count] > $max)
  22. $max = $row[ip_count];
  23. }
  24. while (($row2 = mysql_fetch_array($res4)) != NULL) {
  25. if ($row2[ip_count] > $max)
  26. $max = $row2[ip_count];
  27. }
  28. $st = "SELECT date, COUNT(DISTINCT ip) as ip_count FROM stats WHERE
  29. site = $site AND date >= '" . date("Ymd") . "' - $period + 1 GROUP BY
  30. date ORDER BY date DESC";
  31. $res = mysql_query($st) or die(mysql_error());
  32. $st2 = "SELECT date, COUNT(ip) as ip_count FROM stats WHERE site =
  33. $site GROUP BY date ORDER BY date DESC";
  34. $res2 = mysql_query($st2);
  35.  
  36. while (($row = mysql_fetch_array($res) != NULL) {
  37. ?>
  38. <tr><td><p><?for($i=0;$i<10;$i++)echo $row[date][$i];?></p></td><td
  39. align="center"><p><?=$row['ip_count']?></p></td><td
  40. align="center"><p><?=$row2['ip_count']?></p></td></tr>
  41. <?
  42. }
  43. ?>


Ten post edytował rafnal 13.07.2010, 13:47:35
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
krzysztof_kf
post 13.07.2010, 14:54:31
Post #2





Grupa: Zarejestrowani
Postów: 1 135
Pomógł: 158
Dołączył: 19.03.2009
Skąd: Toruń

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


Za pomocą mysql_fetch_array wyświetlasz poszczególne pola za pomocą numerów kolejnych a za mysql_fetch_assoc możesz się odwoływać do pól rekordów za pomocą nazwa .
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 Aktualny czas: 19.08.2025 - 00:23