Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][SQL] Błędne zapytanie i błędne wyniki w tabeli
Forum PHP.pl > Forum > Przedszkole
slupek
Witam, zapętliłem się, chodzę w kółko od 2 godzin. Może ktoś pomoże:

mam skrypcik, co mi pobiera dane z tebeli, oblicza średnią z otrzymanych wartości - wszystko to dladanych z roku 2008. W związku z nadejściem nowego, chcę uaktualnić skrypt, żeby robił to samo dla 2009. W osobnym pliku wszystko działa świetnie, ale chciałbym, żeby wynik dla 2009 był wyświetlany w kolumnie obok 2008 - i z tym do Was przychodzę. Dodatkowo, skrypt ma wyświetlać dane w odpowiednim kolorze, dla każdego progu (pętelka). Zaznaczam, że podstawą tego skryptu jest skrypt darmowy, nie mojego autorstwa, natomiast ja dokonałem tylko jego modyfikacji dla swoich potrzeb (żeby nie było, że coś sobie przywłaszczam). Próbowałem inkludować plik dla 2009 ale niestety dzieją sie... Po przerobieniu kwerendy z datami dla 2009, pokazuje poprawnie do pierwszego spełnionego warunku, potem już tylko powiela wynik (bez sensu) i ginie funkcja kolorowania progów. Any ideas?

Oto skrypt działający dla 2008 roku:

  1. <?php
  2. /*
  3.  * You may not remove this header!
  4.  *
  5.  * -----------------------------------------------------------------------------------------------------------------------
  6.  * 'pilot_roster.php' script code was made by Paulo Correia - FSAcars Team (Š)2003
  7.  * It's part of the FSAcars 2 for VA's package
  8.  *
  9.  * This script assumes that the VA has PHP support and a MySQL database on it's site
  10.  * ----------------------------------------------------------------------------------------------------------------------
  11.  *
  12.  * FSACARS is distributed freely.
  13.  *
  14.  * Disclaimer
  15.  * Although this product has been intensively tested, the authors accepts no responsibility for any damages caused by the use or misuse of this
  16.  * software. This software is distributed 'as is' with no warranty expressed or implied. The authors will not be responsible for any losses incurred, either directly or indirectly, by the use of this software.
  17.  * Use this software entirely at your own risk. If you do not agree to these terms then you must not use this software.
  18.  *
  19.  * Copyright
  20.  * This software is a copyrighted program. AIBridge is a copyright of José Oliveira. FSACARS coding is a copyright of José Oliveira. FSACARS concept was idealized by Pedro Sousa.
  21.  * It is protected by international copyright laws.
  22.  */
  23.  
  24. /* Constants */
  25. @define ("MYSQL_CONNECT_INCLUDE", "connect_db.php");      // MySQL database connection (a sample file is included)
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. /* Database connection */
  36. include(MYSQL_CONNECT_INCLUDE);
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. /* Select all pilots */
  45. $query = "SELECT * FROM pilots where status = '1' ORDER BY pilot_num ASC";
  46. $result = mysql_query($query);
  47.  
  48. /* Determine the number of pilots */
  49. $number = mysql_numrows($result);
  50.  
  51. if ($number > 0) {
  52.   /* Print roster header */
  53.  
  54.  
  55. print "<table class= 'sample' bgcolor=FFFFFF width=180  border=1 'collapse' style= border-collapse: collapse >";  
  56.   print "<td bgcolor=FFFFFF width=40 height=18 align=center><font face=Arial color=#FFFAF0 size=2><b>Pilot</b></font></td>";
  57. print "<td bgcolor=FFFFFF width=40 height=18 align=center><font face=Arial color=#FFFAF0 size=2><b>Lotów</b></font></td>";
  58.   print "<td bgcolor=FFFFFF width=100 height=18 align=center><font face=Arial color=#FFFAF0 size=2><b>Poziom</b></font></td>";
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /* Get pilots info */
  67.   for ($i=0; $i<$number; $i++) {
  68.           $num = mysql_result($result,$i,"pilot_num");
  69. $csb = mysql_result($result,$i,"csb");
  70.           $name = mysql_result($result,$i, "name");
  71.           $city = mysql_result($result,$i, "city");
  72.           $country = mysql_result($result,$i, "country");
  73.           $status = mysql_result($result,$i, "status");
  74.      $id = mysql_result($result,$i, "pilot_id");
  75.      
  76.       /* Calculate flight hours */
  77.      $query_hours = "SELECT sec_to_time(sum(time_to_sec(t2.duration))) AS duration_sum FROM pilots t1, reports t2 WHERE t1.pilot_id=$id AND t1.pilot_id=t2.pilot_id";
  78.          $result_hours = mysql_query($query_hours);
  79.        
  80.          if (mysql_numrows($result_hours) > 0) {
  81.         $time = mysql_result($result_hours,0,"duration_sum");
  82.  
  83.  
  84.  
  85. // Łączna liczba
  86.  
  87.  
  88.  
  89.  
  90.  
  91. $query_count = "SELECT * FROM `reports` WHERE pilot_id = $id and date > '2007-12-31' AND (zaliczenie = 'A' OR zaliczenie= 'B' OR zaliczenie = 'C') " ;
  92. $result_count = mysql_query($query_count);
  93.  
  94. $value =  mysql_num_rows($result_count);
  95.  
  96.  
  97. if ($value < 1) {
  98.  
  99. print "";
  100.  
  101.  
  102.  
  103. }
  104.  
  105. else {
  106.  
  107.  
  108. $query_count2 = "SELECT * FROM `reports` WHERE pilot_id = $id and zaliczenie = 'A' and date > '2007-12-31' " ;
  109. $result_count2 = mysql_query($query_count2);
  110.  
  111. $value2 =  mysql_num_rows($result_count2);
  112.  
  113.  
  114.  
  115. //Loty warunkowe
  116.  
  117. $query_count3 = "SELECT * FROM `reports` WHERE pilot_id = $id and zaliczenie = 'B'  and date > '2007-12-31' " ;
  118. $result_count3 = mysql_query($query_count3);
  119.  
  120. $value3 =  mysql_num_rows($result_count3);
  121.  
  122.  
  123.  
  124.  
  125. // Obliczanie skutecznosci
  126.  
  127. $skutecznosc = ($value2 * 100)/ $value ;
  128.  
  129. $skutecznosc_zaokraglona = round($skutecznosc, 2);
  130.  
  131.  
  132.  
  133.  
  134.      }  
  135. }
  136.  
  137.  
  138.   /* Print roster header */
  139.    
  140.  
  141.  
  142.  if ($value < 1) {
  143.  
  144. print "<tr>";
  145.           print "<td width=16% height=18 align=left><font face=Arial size=2 color=#FF0000 >$csb</font></td>";
  146. print "<td width=16% height=18 align=center><font face=Arial size=2 color=#FF0000 >$value</font></td>";
  147.           print "<td width=20% height=18 align=left><font face=Arial size=2 color=#FF0000 >nd</font></td>";
  148.  
  149. }
  150.  
  151. else if ($skutecznosc_zaokraglona < 65) {
  152.    
  153.      print "<tr>";
  154.           print "<td width=16% height=18 align=left><font face=Arial size=2 color=#FF0000 >$csb</font></td>";
  155. print "<td width=16% height=18 align=center><font face=Arial size=2 color=#FF0000 >$value</font></td>";
  156.           print "<td width=20% height=18 align=left><font face=Arial size=2 color=#FF0000 >$skutecznosc_zaokraglona %</font></td>";
  157.                    
  158.  
  159.  
  160. } else if ($skutecznosc_zaokraglona < 75) {
  161.  
  162.  
  163.      print "<tr>";
  164.           print "<td width=16% height=18 align=left><font face=Arial size=2 color=#FFD700 >$csb</font></td>";
  165. print "<td width=16% height=18 align=center><font face=Arial size=2 color=#FFD700 >$value</font></td>";
  166.           print "<td width=20% height=18 align=left><font face=Arial size=2 color=#FFD700 >$skutecznosc_zaokraglona %</font></td>";
  167.                    
  168.  
  169.  
  170.  
  171. } else if ($skutecznosc_zaokraglona < 90) {
  172.  
  173.  
  174.      print "<tr>";
  175.           print "<td width=16% height=18 align=left><font face=Arial size=2 color=#DAA520 >$csb</font></td>";
  176. print "<td width=16% height=18 align=center><font face=Arial size=2 color=#DAA520 >$value</font></td>";
  177.           print "<td width=20% height=18 align=left><font face=Arial size=2 color=#DAA520 >$skutecznosc_zaokraglona %</font></td>";
  178.                    
  179.  
  180. }
  181.  
  182.  
  183. else if ($skutecznosc_zaokraglona < 110) {
  184.  
  185.  
  186.      print "<tr>";
  187.           print "<td width=16% height=18 align=left><font face=Arial size=2 color=#00FF00 >$csb</font></td>";
  188. print "<td width=16% height=18 align=center><font face=Arial size=2 color=#00FF00 >$value</font></td>";
  189.           print "<td width=20% height=18 align=left><font face=Arial size=2 color=#00FF00 >$skutecznosc_zaokraglona %</font></td>";
  190.                    
  191.  
  192. }
  193.    
  194.   print "</tr>";
  195.  
  196.  
  197.    
  198. }
  199.  
  200.  
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. print "</center>";
  210.  
  211. /* Close the database connection */
  212. php?>
memory
pobierasz z bazy tylko rok np

  1. <?php
  2. SELECT DISTINCT year(data) as dat FROM cos
  3.  
  4. if(mysql_num_rows($wyni) <1) {
  5. echo 'brak danych w archiwum';
  6. } else {
  7.  
  8. while($row=mysql_fetch_array($wyni))
  9.    {
  10.    $year =$row['dat'];
  11. }
  12. ?>
w tej samej petli wykonujesz $query_count gdzie data to WHERE YEAR(data) = $year
slupek
Niestety, zwraca mi błędy, zbyt to zamotane dla mnie, a takie proste się wydaje. Próbowałem na swój chłopski rozum dać to tak między linie 88 i 139 z pierwszego postu, ale liczy błędnie:

  1. <?php
  2. // laczna liczba 2008
  3.  
  4.  
  5.  
  6.  
  7.  
  8. $query_count = "SELECT * FROM `reports` WHERE pilot_id = $id and date > '2007-12-31' AND (zaliczenie = 'A' OR zaliczenie= 'B' OR zaliczenie = 'C') " ;
  9. $result_count = mysql_query($query_count);
  10.  
  11. $value =  mysql_num_rows($result_count);
  12.  
  13.  
  14.  
  15.  
  16.  
  17. $query_count2 = "SELECT * FROM `reports` WHERE pilot_id = $id and zaliczenie = 'A' and date > '2007-12-31' " ;
  18. $result_count2 = mysql_query($query_count2);
  19.  
  20. $value2 =  mysql_num_rows($result_count2);
  21.  
  22.  
  23.  
  24. //Loty warunkowe
  25.  
  26. $query_count3 = "SELECT * FROM `reports` WHERE pilot_id = $id and zaliczenie = 'B'  and date > '2007-12-31' " ;
  27. $result_count3 = mysql_query($query_count3);
  28.  
  29. $value3 =  mysql_num_rows($result_count3);
  30.  
  31.  
  32.  
  33.  
  34. // Obliczanie skutecznosci
  35.  
  36. $skutecznosc = ($value2 * 100)/ $value ;
  37.  
  38. $skutecznosc_zaokraglona = round($skutecznosc, 2);
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. // laczna liczba2009
  49.  
  50.  
  51.  
  52. $query_count9 = "SELECT * FROM `reports` WHERE pilot_id = $id and date > '2008-12-31' AND (zaliczenie = 'A' OR zaliczenie= 'B' OR zaliczenie = 'C') " ;
  53. $result_count9 = mysql_query($query_count9);
  54.  
  55. $value9 =  mysql_num_rows($result_count9);
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. $query_count29 = "SELECT * FROM `reports` WHERE pilot_id = $id and zaliczenie = 'A' and date > '2008-12-31' " ;
  63. $result_count29 = mysql_query($query_count29);
  64.  
  65. $value29 =  mysql_num_rows($result_count29);
  66.  
  67.  
  68.  
  69. //Loty warunkowe
  70.  
  71. $query_count39 = "SELECT * FROM `reports` WHERE pilot_id = $id and zaliczenie = 'B'  and date > '2008-12-31' " ;
  72. $result_count39 = mysql_query($query_count39);
  73.  
  74. $value39 =  mysql_num_rows($result_count39);
  75.  
  76.  
  77.  
  78.  
  79. // Obliczanie skutecznosci
  80.  
  81. $skutecznosc9 = ($value29 * 100)/ $value9 ;
  82.  
  83. $skutecznosc_zaokraglona9 = round($skutecznosc9, 2);
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.    
  94. }
  95. ?>
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.