Advertisement
Guest User

distance_php

a guest
Apr 12th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. <?php
  2. $page = $_SERVER['PHP_SELF'];
  3. $sec = "5";
  4. header("Refresh: $sec; url=$page");
  5.  
  6. $servername = "localhost";
  7. $username = "id8804958_user";
  8. $password = "taimekasvatamine";
  9. $dbname = "id8804958_taimekasvatamine";
  10.  
  11. // Create connection
  12. $conn = new mysqli($servername, $username, $password, $dbname);
  13. // Check connection
  14. if ($conn->connect_error) {
  15. die("Connection failed: " . $conn->connect_error);
  16. }
  17. if(!empty($_GET['distance']) || !empty($_GET['light']) ){
  18. $sql = "INSERT INTO Distance (Distance, Light)
  19. VALUES (".htmlspecialchars($_GET["distance"]).",".htmlspecialchars($_GET["light"]).")";
  20.  
  21. if ($conn->query($sql) === TRUE) {
  22. echo "New record created successfully";
  23.  
  24. } else {
  25. echo "Error: " . $sql . "<br>" . $conn->error;
  26. }
  27. $conn->close();
  28. }
  29. else{
  30. $sql2 = "SELECT Light FROM Distance ORDER BY ID DESC LIMIT 1";
  31. $lightresult = $conn->query($sql2);
  32. while($row = $lightresult->fetch_assoc()) {
  33. echo $row["Light"]. " valgusühikut. <br> <br>Skaalal 1-5 (valgest-pimedani). <br><br>";
  34. if(floatval($row["Light"]) < 1){
  35. echo " Liiga valge lambi jaoks <br>";
  36. $myfile = fopen("led.txt", "w") or die("Unable to open file!");
  37. $txt = "OFF";
  38. fwrite($myfile, $txt);
  39. fclose($myfile);
  40. } else {
  41.  
  42. echo " Vajuta värvi vahetamiseks nuppu <br>";
  43.  
  44. $myfile = fopen("led.txt", "w") or die("Unable to open file!");
  45. $txt = "green";
  46. if (isset( $_POST['OFF'])){
  47. $txt = "OFF";
  48. }else if(isset($_POST['rainbow'])){
  49. $txt = "rainbow";
  50. }else if(isset($_POST['red'])){
  51. $txt = "red";
  52. }else if(isset($_POST['blue'])){
  53. $txt = "blue";
  54. }else if(isset($_POST['green'])){
  55. $txt = "green";
  56. }else if(isset($_POST['yellow'])){
  57. $txt = "yellow";
  58. }else if(isset($_POST['purple'])){
  59. $txt = "purple";
  60. }else if(isset($_POST['aqua'])){
  61. $txt = "aqua";
  62. }
  63. fwrite($myfile, $txt);
  64.  
  65. fclose($myfile);
  66. }
  67. }
  68. }
  69.  
  70. $sql3 = "SELECT Date(Time) as kuupaev, (SELECT Distance FROM Distance AS D2 WHERE Date(Time)=Date(D1.Time) GROUP BY Distance ORDER BY COUNT(*) DESC LIMIT 1) AS pikkus From Distance as D1 GROUP BY Date(D1.Time)";
  71. $result = $conn->query($sql3); //ÕIGE
  72.  
  73. $csvfile = fopen('plantheight.csv', 'w');
  74. if ($result->num_rows > 0) {
  75. // output data of each row
  76. while($row = $result->fetch_assoc()) {
  77. fputcsv($csvfile, $row);
  78. }
  79. } else {
  80. // echo "0 results";
  81. }
  82. fclose($csvfile);
  83.  
  84. $conn->close();
  85. ?>
  86. <!DOCTYPE html>
  87. <html>
  88. <body>
  89. <h3>Tulukesed</h3>
  90. <form action="" method="POST">
  91. <input type="submit" name="OFF">Väljas <br>
  92. <input type="submit" name="rainbow">Vikerkaar <br>
  93. <input type="submit" name="red">Punane <br>
  94. <input type="submit" name="blue">Sinine <br>
  95. <input type="submit" name="green">Roheline <br>
  96. <input type="submit" name="yellow">Kollane <br>
  97. <input type="submit" name="purple">Lilla <br>
  98. <input type="submit" name="aqua">Aqua
  99. </form>
  100.  
  101. </body>
  102. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement