Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Zmiana pobieraniadanych
globalinfo
post 13.12.2016, 12:27:40
Post #1





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 8.10.2006

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


Jak przerobić poniższy kod aby dane były pobierane z bazy MySQL?
Teraz dane są pobierane z plików TXT

Dane mam zapisywane do bazy i do plików TXT przez skryp add_data.php


Prośba o pomoc sciana.gif


  1. <?php
  2. $id = $_GET['id']; $temp = array(); $all = false;
  3. // CONGIG
  4. $limit_after = 1000; // po ilu pomiarach zacznie zmieniac dokladnosc
  5. $max_res = 10000; // ile pomiarow maksymalnie wyswietli
  6. // name to oczywiscie wyswietlana nazwa, all to czy pokazywac na wspolnym wykresie
  7.  
  8. $t3231= $_GET['t3231'];
  9. $TZ= $_GET['TZ'];
  10. $WZ= $_GET['WZ'];
  11. $CA= $_GET['CA'];
  12. $TCA= $_GET['TCA'];
  13. $WB= $_GET['WB'];
  14. $DAL1= $_GET['DAL1'];
  15. $PWY= $_GET['PWY'];
  16. $SWY= $_GET['SWY'];
  17. $SWE= $_GET['SWE'];
  18. $BWE= $_GET['BWE'];
  19.  
  20. $temp[0] = array("name" => "t3231", "all" => $t3231);
  21. $temp[1] = array("name" => "Temperatura na zewnÄ…trz", "all" => $TZ);
  22. $temp[2] = array("name" => "Wilgotność na zewnątrz", "all" => $WZ);
  23. $temp[3] = array("name" => "Ciśnienie atmosferyczne", "all" => $CA);
  24. $temp[4] = array("name" => "TCiśnienie", "all" => $TCA);
  25. $temp[5] = array("name" => "Woda Bojler", "all" => $WB);
  26. $temp[6] = array("name" => "Pokoje", "all" => $DAL1);
  27. $temp[7] = array("name" => "Piec wy", "all" => $PWY);
  28. $temp[8] = array("name" => "Solar wy", "all" => $SWY);
  29. $temp[9] = array("name" => "Solar we", "all" => $SWE);
  30. $temp[10] = array("name" => "Bojler we", "all" => $BWE);
  31.  
  32.  
  33. // Koniec Configu
  34. // obecna temp
  35. foreach(unserialize(file_get_contents("ostatnia.txt")) as $i => $t) {
  36. if(isset($temp[($i)]))
  37. $temp[($i)]["temp"] = $t; // Obecna Temp
  38. }
  39. if(!isset($temp[($id)])) // Gdy brak id to pokazuj wszystkie czujniki z parametrem all = true
  40. $all = true;
  41.  
  42. function getData($id) {
  43. global $limit_after,$max_res;
  44. $d = file_get_contents($id.'.txt');
  45. $data = array(); $t = ""; $type = 0;
  46. for($i = 0; $i < strlen($d); $i++) {
  47. if($d[$i] == '[') {
  48. $t = ""; $type = 1;
  49. } else if($d[$i] == ',' && $type == 1) {
  50. $time = $t; $type = 2; $t = "";
  51. } else if($d[$i] == ']' && $type == 2) {
  52. $data[trim($time)] = trim($t); $type = 0; $t = "";
  53. } else
  54. $t .= $d[$i];
  55. }
  56. $ile = count($data); $t = "";
  57. $lim = ($ile-$limit_after)/$max_res;
  58. $prog = 0; $lprog = 0;
  59. foreach($data as $time => $temp) {
  60. if($prog+$limit_after > $ile) {
  61. $t .= "[".$time."000,".$temp."],";
  62. } else {
  63. if(floor($lprog) <= floor($prog)) {
  64. $t .= "[".$time."000,".$temp."],";
  65. $lprog += $lim;
  66. }
  67. }
  68. $prog++;
  69. }
  70. return rtrim($t, ",");
  71. }
  72. ?>
  73. ....


Dane mam zapisywane do plików i bazy w taki sposób.

Plik add_data.php
  1. <?php
  2.  
  3. $czas = time();
  4.  
  5. // Connect to MySQL
  6. include("dbconnect.php");
  7.  
  8. // Prepare the SQL statement
  9. $query = "INSERT INTO energie (czas, t3231, tDHT11, wilgotnosc, cisnienie, tcisnienie, dallasnr0, dallasnr1, dallasnr2, dallasnr3, dallasnr4, dallasnr5)
  10. VALUES ('$czas', '$_POST[t3231]', '$_POST[tDHT11]',
  11. '$_POST[wilgotnosc]', '$_POST[cisnienie]', '$_POST[tcisnienie]',
  12. '$_POST[dallasnr0]', '$_POST[dallasnr1]' , '$_POST[dallasnr2]' , '$_POST[dallasnr3]' , '$_POST[dallasnr4]' , '$_POST[dallasnr5]' )";
  13.  
  14. // Go to the review_data.php (optional)
  15. //header("Location: review_data.php");
  16.  
  17. if(!@mysql_query($query))
  18. {
  19. echo "&Answer; SQL Error - ".mysql_error();
  20. return;
  21.  
  22. }
  23. $x=0;
  24. //$czas = time();
  25.  
  26. for( $x = 0; $x < 11; $x++ )
  27. {
  28.  
  29. if ($x==0){ $wartosc=$_POST[t3231];}
  30. if ($x==1){ $wartosc=$_POST[tDHT11];}
  31. if ($x==2){ $wartosc=$_POST[wilgotnosc];}
  32. if ($x==3){ $wartosc=$_POST[cisnienie];}
  33. if ($x==4){ $wartosc=$_POST[tcisnienie];}
  34. if ($x==5){ $wartosc=$_POST[dallasnr0];}
  35. if ($x==6){ $wartosc=$_POST[dallasnr1];}
  36. if ($x==7){ $wartosc=$_POST[dallasnr2];}
  37. if ($x==8){ $wartosc=$_POST[dallasnr3];}
  38. if ($x==9){ $wartosc=$_POST[dallasnr4];}
  39. if ($x==10){ $wartosc=$_POST[dallasnr5];}
  40.  
  41.  
  42. //$t2=".00";
  43.  
  44. $wartosc = ($wartosc / 100 );
  45. $wartosc = number_format($wartosc, 2, '.', '');
  46. //$string = strlen($wartosc);
  47. //if ($string==2){$wartosc=$wartosc.$t2;}
  48. //if ($string==3){$wartosc=$wartosc.$t2;}
  49.  
  50. // zmienna $dane, która będzie zapisana
  51. // może także pochodzić z formularza np. $dane = $_POST['dane'];
  52. $dane = "[$czas,$wartosc],\n";
  53.  
  54. // przypisanie zmniennej $file nazwy pliku
  55. $file = "$x.txt";
  56.  
  57. // uchwyt pliku, otwarcie do dopisania
  58. $fp = fopen($file, "a");
  59.  
  60. // blokada pliku do zapisu
  61. flock($fp, 2);
  62.  
  63. // zapisanie danych do pliku
  64. fwrite($fp, $dane);
  65.  
  66. // odblokowanie pliku
  67. flock($fp, 3);
  68.  
  69. // zamknięcie pliku
  70. fclose($fp);
  71. echo $x;
  72. echo "<br/>";
  73.  
  74. }
  75. ?>
Go to the top of the page
+Quote Post

Posty w temacie


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 Wersja Lo-Fi Aktualny czas: 17.06.2025 - 16:51