Guest User

Untitled

a guest
Nov 13th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.63 KB | None | 0 0
  1. <?php
  2.  
  3. // First we execute our common code to connection to the database and start the session
  4. require("common.php");
  5.  
  6. ?>
  7.  
  8. <!DOCTYPE HTML>
  9. <!--
  10. Read Only by HTML5 UP
  11. html5up.net | @n33co
  12. Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
  13. -->
  14. <html>
  15. <head>
  16. <title>Pet Food Level Tracker</title>
  17. <meta charset="utf-8" />
  18. <meta name="viewport" content="width=device-width, initial-scale=1" />
  19.  
  20. </head>
  21. <body>
  22.  
  23.  
  24.  
  25. <!-- Header -->
  26. <section id="header">
  27. <header>
  28. <?php
  29. if(!empty($_SESSION['user']))
  30. {
  31. echo '<a href="logout.php"
  32. data-ajax="false">Logout</a>';
  33. echo "<br>Hello, ";
  34. echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
  35. echo "!<br><br>";
  36. }
  37. else
  38. {
  39. echo '<div data-role="main" class="ui-content">
  40. <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left">Login</a>
  41. <div data-role="popup" id="myPopup" class="ui-content" style="min-width:250px;">
  42.  
  43.  
  44. <h1>Login</h1>
  45. <form action="login.php" method="post" data-ajax="false">
  46. <div>
  47. <label for="usrnm" class="ui-hidden-accessible">Username:</label>
  48. <input type="text" name="username" id="usrnm" placeholder="Username">
  49. <label for="pswd" class="ui-hidden-accessible">Password:</label>
  50. <input type="password" name="password" id="pswd" placeholder="Password">
  51. <input type="submit" data-inline="true" value="Log in">
  52.  
  53. </div>
  54. </form>
  55. </div>
  56. <br>
  57. <a href="register.php"
  58. data-ajax="false">Register</a><br><br>';
  59.  
  60. }
  61. ?>
  62. <span class="image avatar"><img src="images/avatar.jpg" alt="" /></span>
  63. <h1 id="logo"><a href="#">Pet Food Level Tracker</a></h1>
  64. <!--<p>Tracking how much your pets devoured<br/>
  65. since 2016<br/>-->
  66. <?php
  67. if(!empty($_SESSION['user']))
  68. {
  69. echo "Tracking how much ";
  70. echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
  71. echo "'s pets devoured since 2016.<br/>";
  72. }
  73. else
  74. {
  75. echo "Tracking how much your pets devoured since 2016.";
  76. }
  77. ?>
  78. </header>
  79. <nav id="nav">
  80. <ul>
  81. <li><a href="#one">Food Tracker</a></li>
  82. <li><a href="#two">Settings</a></li>
  83. <li><a href="#three">About</a></li>
  84. <li><a href="http://www.fajnyblog.com">Blog</a></li> linka walnac tu
  85. </ul>
  86. </nav>
  87. <footer>
  88. <ul class="icons">
  89. <li><a href="#" class="icon fa-github"><span class="label">Github</span></a></li>
  90. <li><a href="#" class="icon fa-envelope"><span class="label">Email</span></a></li>
  91. </ul>
  92. </footer>
  93. </section>
  94.  
  95. <!-- Wrapper -->
  96. <div id="wrapper">
  97.  
  98. <!-- Main -->
  99. <div id="main">
  100.  
  101. <!-- One -->
  102. <section id="one">
  103. <div class="container">
  104. <header class="major">
  105. <h2>Food Tracker</h2>
  106. <h4>Values:</h4>
  107.  
  108.  
  109. <?php
  110. if(!empty($_SESSION['user']))
  111. {
  112. $uid = $_SESSION['user']['id'];
  113. $query = "
  114. SELECT
  115. device_id
  116. FROM user_devices
  117. WHERE
  118. user_id = :uid
  119. ";
  120. $query_params = array(
  121. ':uid' => $uid,
  122. );
  123.  
  124. try
  125. {
  126. $stmt = $db->prepare($query);
  127. $result = $stmt->execute($query_params);
  128. }
  129. catch(PDOException $ex)
  130. {
  131. die("Please don't leave anything blank!");
  132. }
  133. $row = $stmt->fetch();
  134.  
  135. if($row)
  136. {
  137. $query = "
  138. SELECT
  139. timestamp,
  140. food_level,
  141. temp_level,
  142. humidity_level
  143. FROM device_values
  144. WHERE
  145. device_id = :devid
  146. ";
  147. $query_params = array(
  148. ':devid' => $row['device_id'],
  149. );
  150.  
  151. try
  152. {
  153. $stmt = $db->prepare($query);
  154. $result = $stmt->execute($query_params);
  155. }
  156. catch(PDOException $ex)
  157. {
  158. die("Please don't leave anything blank!");
  159. }
  160. $timestampes = [];
  161. $food_levels = [];
  162.  
  163. echo '<table border="1">
  164. <tbody>
  165. <tr>
  166. <td>timestamp</td>
  167. <td>food_level</td>
  168. <td>temp_level</td>
  169. <td>humidity_level</td>
  170. </tr>
  171. ';
  172.  
  173. while($devices_row = $stmt->fetch())
  174. {
  175. echo '<tr><td>';
  176. echo $devices_row['timestamp'];
  177. array_push($timestampes, $devices_row['timestamp']);
  178. echo '</td><td>';
  179. echo $devices_row['food_level'];
  180. array_push($food_levels, $devices_row['food_level']);
  181. echo '</td><td>';
  182. echo $devices_row['temp_level'];
  183. echo '</td><td>';
  184. echo $devices_row['humidity_level'];
  185. echo '</td></tr>';
  186. }
  187. echo '</tr>
  188. </tbody>
  189. </table>';
  190. }
  191. }
  192.  
  193. ?>
  194. </header>
  195. <p>
  196. <canvas id="myChart" width="300" height="300"></canvas>
  197. <canvas id="myChartTEMP" width="300" height="300"></canvas>
  198. </p>
  199.  
  200. <section id="two">
  201. <div class="container">
  202. <header class="major">
  203. <h2>Settings</h2>
  204. </header>
  205. <p>
  206. <p>
  207. <?php
  208.  
  209. if(!empty($_SESSION['user']))
  210. {
  211. echo 'Here you can set minimum amount of food, temperature and humidity levels, below which site will send a notification to your phone. You can also set a reminder for changing water in water bottle.';
  212. echo '<br><br>Your devices:<br><br>';
  213. $uid = $_SESSION['user']['id'];
  214.  
  215. $query = "
  216. SELECT
  217. device_id
  218. FROM user_devices
  219. WHERE
  220. user_id = :uid
  221. ";
  222. $query_params = array(
  223. ':uid' => $uid,
  224. );
  225.  
  226. try
  227. {
  228. $stmt = $db->prepare($query);
  229. $result = $stmt->execute($query_params);
  230. }
  231. catch(PDOException $ex)
  232. {
  233. die("Please don't leave anything blank!");
  234. }
  235. while($devices_row = $stmt->fetch())
  236. {
  237.  
  238. $query = "
  239. SELECT
  240. device_name,
  241. last_update
  242. FROM devices
  243. WHERE
  244. device_id = :devid
  245. ";
  246. $query_params = array(
  247. ':devid' => $devices_row['device_id'],
  248. );
  249. try
  250. {
  251. $stmt2 = $db->prepare($query);
  252. $result = $stmt2->execute($query_params);
  253. }
  254. catch(PDOException $ex)
  255. {
  256. die("Please don't leave anything blank!");
  257. }
  258.  
  259. $device_info = $stmt2->fetch();
  260. if($device_info)
  261. {
  262. $query = "
  263. SELECT
  264. food_min,
  265. next_water_check,
  266. temp_min,
  267. temp_max,
  268. humidity_min,
  269. humidity_max
  270. FROM user_settings
  271. WHERE
  272. device_id = :device_id
  273. ";
  274.  
  275. $query_params = array(
  276. ':device_id' => $devices_row['device_id'],
  277. );
  278.  
  279. try
  280. {
  281. $stmt3 = $db->prepare($query);
  282. $result = $stmt3->execute($query_params);
  283. }
  284. catch(PDOException $ex)
  285. {
  286. die("Please don't leave anything blank!");
  287. }
  288. $device_settings = $stmt3->fetch();
  289. echo 'ID: ';
  290. echo $devices_row['device_id'];
  291. echo '<br>Device name: ';
  292. echo $device_info['device_name'];
  293. echo '<br>Last update: ';
  294. echo $device_info['last_update'];
  295. echo '<form action="insert.php" method="post" data-ajax="false">';
  296. echo '<input type="hidden" name="device_id" id="device_id" value="' ;
  297. echo $devices_row['device_id'];
  298. echo '"';
  299. echo '<br>Minimum amount of food in grams: <input type="number" min="1" name="food_min" id="food_min" value="';
  300. echo $device_settings['food_min'];
  301. echo '"><br>Next water level check on: <input type="timestamp" name="next_water_check" min="1" id="next_water_check" value="';
  302. echo $device_settings['next_water_check'];
  303. /*if ($device_settings['next_water_check'] != null)
  304. {
  305. echo $device_settings['next_water_check'];
  306. }
  307. else
  308. {
  309. $date = new DateTime();
  310. echo $date->format(Y-m-d H:i:s') . "\n";
  311. }*/
  312. echo '"><br>Temperature in Celsius degrees between: <input type="number" name="temp_min" min="1" max="50" id="temp_min" value="';
  313. echo $device_settings['temp_min'];
  314. echo '"><br>and: <input type="number" name="temp_max" min="1" max="50" id="temp_max" value="';
  315. echo $device_settings['temp_max'];
  316. echo '"><br>Humidity in % between:<input type="number" name="humidity_min" min="1" max="100" id="humidity_min" value="';
  317. echo $device_settings['humidity_min'];
  318. echo '"><br>and:<input type="number" name="humidity_max" min="1" max="100" id="humidity_max" value="';
  319. echo $device_settings['humidity_max'];
  320. echo '"><br>';
  321. echo '<input type="submit" value="Submit">
  322. </form>';
  323. }}
  324.  
  325.  
  326. {
  327.  
  328. echo '<form action="devices.php" method="post" data-ajax="false">';
  329. echo 'Add new device.';
  330. echo '<br>Device name: <input type="text" name="device_name" id="device_name" value="">';
  331. echo '<br>Device ID: <input type="number" name="device_id" id="device_id" value="">';
  332. echo '<input type = "hidden" name="food_min" id="food_min" value= "0">';
  333. echo '<input type = "hidden" name="next_water_check" id="next_water_check" value= "0">';
  334. echo '<input type = "hidden" name="temp_min" id="temp_min" value= "0">';
  335. echo '<input type = "hidden" name="temp_max" id="temp_max" value= "1">';
  336. echo '<input type = "hidden" name="humidity_min" id="humidity_min" value= "0">';
  337. echo '<input type = "hidden" name="humidity_max" id="humidity_max" value= "0">';
  338. echo '<input type="submit" value="Add device">
  339. </form>';}
  340.  
  341.  
  342. }
  343. else
  344. {
  345. echo "Content here is available only to members. Please register and login!";
  346. }
  347. ?>
  348. </p>
  349. </div>
  350. </section>
  351.  
  352. </div>
  353. </section>
  354. <section id="three">
  355. <div class="container">
  356. <header class="major">
  357. <h2>About</h2>
  358. <p>opis</p>
  359. </header>
  360. <p>
  361. chyba niepotrzebne
  362.  
  363. </p>
  364. </div>
  365. </section>
  366.  
  367. </div>
  368.  
  369.  
  370. <!-- Footer -->
  371. <section id="footer">
  372. <div class="container">
  373. <ul class="copyright">
  374. <li>&copy; Untitled. All rights reserved.</li><li>Design: <a href="http://html5up.net">HTML5 UP</a></li>
  375. </ul>
  376. </div>
  377. </section>
  378.  
  379. </div>
  380.  
  381. <!-- Scripts -->
  382. <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
  383. <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  384. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  385.  
  386. <script src="assets/js/jquery.scrolly.min.js"></script>
  387. <script src="assets/js/skel.min.js"></script>
  388. <script src="assets/js/util.js"></script>
  389. <!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
  390. <script src="assets/js/main.js"></script>
  391. <script src="assets/js/Chart.js"></script>
  392. <!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->
  393. <link rel="stylesheet" href="assets/css/main.css" />
  394.  
  395. <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  396. <!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->
  397.  
  398. <script>
  399. var ctx = $("#myChart");
  400.  
  401. var myChart = new Chart(ctx, {
  402. type: 'line',
  403. data: {
  404. labels: [<?php
  405. foreach ($timestampes as $value)
  406. {
  407. echo '"';
  408. echo $value;
  409. echo '", ';
  410. }?>
  411. ],
  412. datasets: [{
  413. label: 'Food level',
  414. data: [<?php
  415. foreach ($food_levels as $value)
  416. {
  417. echo $value;
  418. echo ', ';
  419. }?>
  420. ],
  421. backgroundColor: [
  422. 'rgba(255, 99, 132, 0.2)',
  423. 'rgba(54, 162, 235, 0.2)',
  424. 'rgba(255, 206, 86, 0.2)',
  425. 'rgba(75, 192, 192, 0.2)',
  426. ],
  427. borderColor: [
  428. 'rgba(255,99,132,1)',
  429. 'rgba(54, 162, 235, 1)',
  430. 'rgba(255, 206, 86, 1)',
  431. 'rgba(75, 192, 192, 1)',
  432. ],
  433. borderWidth: 1
  434. }]
  435. },
  436. options: {
  437. scales: {
  438. yAxes: [{
  439. ticks: {
  440. beginAtZero:true
  441. }
  442. }]
  443. }
  444. }
  445. });
  446.  
  447. </script>
  448.  
  449. <script>
  450. var ctx = $("#myChartTEMP");
  451.  
  452. var myChartTEMP = new Chart(ctx, {
  453. type: 'line',
  454. data: {
  455. labels: [<?php
  456. foreach ($timestampes as $value)
  457. {
  458. echo '"';
  459. echo $value;
  460. echo '", ';
  461. }?>
  462. ],
  463. datasets: [{
  464. label: 'Temperature level',
  465. data: [<?php
  466. foreach ($temp_level as $value)
  467. {
  468. echo $value;
  469. echo ', ';
  470. }?>
  471. ],
  472. backgroundColor: [
  473. 'rgba(255, 99, 132, 0.2)',
  474. 'rgba(54, 162, 235, 0.2)',
  475. 'rgba(255, 206, 86, 0.2)',
  476. 'rgba(75, 192, 192, 0.2)',
  477. ],
  478. borderColor: [
  479. 'rgba(255,99,132,1)',
  480. 'rgba(54, 162, 235, 1)',
  481. 'rgba(255, 206, 86, 1)',
  482. 'rgba(75, 192, 192, 1)',
  483. ],
  484. borderWidth: 1
  485. }]
  486. },
  487. options: {
  488. scales: {
  489. yAxes: [{
  490. ticks: {
  491. beginAtZero:true
  492. }
  493. }]
  494. }
  495. }
  496. });
  497.  
  498. </script>
  499.  
  500. </body>
  501. </html>
Add Comment
Please, Sign In to add comment