Simple PHP & SQL weight tracker for multiple persons
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

weight.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /*
  3. * When calleld as is, return JSON object like this:
  4. * {
  5. * "users": [
  6. * {
  7. * "name": "User 1",
  8. * "id": 0,
  9. * "data": [
  10. * {
  11. * "x": "2018-10-29 20:30",
  12. * "y": "110.0"
  13. * },
  14. * {
  15. * "x": "2018-10-30 20:30",
  16. * "y": "110.5"
  17. * }
  18. * ]
  19. * },
  20. * {
  21. * "name": "User 2",
  22. * "id": 1,
  23. * "data": [
  24. * {
  25. * "x": "2018-10-29 20:30",
  26. * "y": "105.5"
  27. * },
  28. * {
  29. * "x": "2018-10-30 20:30",
  30. * "y": "105.0"
  31. * }
  32. * ]
  33. * }
  34. * ]
  35. * }
  36. * or
  37. * {
  38. * "error": "No configuration found"
  39. * }
  40. *
  41. * When called with POST parameters, add user or data-point to db
  42. * parameters: username, (date, time, weight)
  43. */
  44. function print_json_error($s) {
  45. echo '{';
  46. echo ' "error": "' . $s;
  47. if (mysql_errno()) {
  48. echo ' (' . mysql_error() . ')';
  49. }
  50. echo '"';
  51. echo '}';
  52. }
  53. function print_html($s) {
  54. echo '<html lang="en">';
  55. echo ' <head>';
  56. echo ' <meta charset="utf-8" />';
  57. echo ' <title>Weight-Track DB</title>';
  58. echo ' </head>';
  59. echo ' <body>';
  60. echo ' <p>' . $s . '</p>';
  61. if (mysql_errno()) {
  62. echo '<p>MySQL Error: "' . mysql_error() . '"</p>';
  63. }
  64. echo ' <a href="index.html">Back to Main-Page...</a>';
  65. echo ' </body>';
  66. echo '</html>';
  67. }
  68. function print_error($s) {
  69. if (isset($_POST['username'])) {
  70. return print_html($s);
  71. } else {
  72. return print_json_error($s);
  73. }
  74. }
  75. include('config.php');
  76. if ((!isset($sql_host))
  77. || (!isset($sql_username))
  78. || (!isset($sql_password))
  79. || (!isset($sql_database))) {
  80. print_error('Configuration Error');
  81. exit(1);
  82. }
  83. $db = mysql_connect($sql_host, $sql_username, $sql_password);
  84. mysql_select_db($sql_database);
  85. if (mysql_errno()) {
  86. print_error('Database Error');
  87. exit(1);
  88. }
  89. $sql = 'CREATE TABLE IF NOT EXISTS weight_users (';
  90. $sql .= 'id int NOT NULL AUTO_INCREMENT,';
  91. $sql .= 'name varchar(255),';
  92. $sql .= 'PRIMARY KEY (id)';
  93. $sql .= ');';
  94. $result = mysql_query($sql);
  95. if (!$result) {
  96. print_error('Error (re-) creating database table for users!');
  97. exit(1);
  98. }
  99. $sql = 'CREATE TABLE IF NOT EXISTS weight_data (';
  100. $sql .= 'id int NOT NULL,';
  101. $sql .= 'date DATETIME,';
  102. $sql .= 'weight DECIMAL(5,2)';
  103. $sql .= ');';
  104. $result = mysql_query($sql);
  105. if (!$result) {
  106. print_error('Error (re-) creating database table for weights!');
  107. exit(1);
  108. }
  109. if (isset($_POST['username'])
  110. && isset($_POST['date'])
  111. && isset($_POST['time'])
  112. && isset($_POST['weight'])) {
  113. $sql = 'INSERT INTO weight_data(id, date, weight) VALUES (';
  114. $sql .= mysql_real_escape_string(str_replace('user_', '', $_POST['username']));
  115. $sql .= ', ';
  116. $datetime = $_POST['date'] . $_POST['time'];
  117. $timestamp = strtotime($datetime);
  118. if (($timestamp == FALSE) || ($timestamp == -1)) {
  119. print_error('Error interpreting DateTime: "' . $datetime . '"');
  120. exit(1);
  121. }
  122. $mysqltime = date("Y-m-d H:i:s", $timestamp);
  123. $sql .= '"' . $mysqltime . '", ';
  124. $sql .= mysql_real_escape_string($_POST['weight']) . ')';
  125. $result = mysql_query($sql);
  126. if (!$result) {
  127. print_error('Error adding new data for user "' . $_POST['username'] . '" to DB! ' . $sql);
  128. } else {
  129. print_error('Added new data for user "' . $_POST['username'] . '" to DB!');
  130. }
  131. } else if (isset($_POST['username'])) {
  132. $sql = 'INSERT INTO weight_users(name) VALUES ("';
  133. $sql .= mysql_real_escape_string($_POST['username']);
  134. $sql .= '")';
  135. $result = mysql_query($sql);
  136. if (!$result) {
  137. print_error('Error adding new user "' . $_POST['username'] . '" to DB!');
  138. } else {
  139. print_error('Added new user "' . $_POST['username'] . '" to DB!');
  140. }
  141. } else if (isset($_GET['debug'])) {
  142. echo <<<EOF
  143. {
  144. "users": [
  145. {
  146. "name": "User 1",
  147. "id": 0,
  148. "data": [
  149. {
  150. "x": "2018-10-27 20:30",
  151. "y": "110.0"
  152. },
  153. {
  154. "x": "2018-10-28 20:30",
  155. "y": "110.7"
  156. },
  157. {
  158. "x": "2018-10-29 20:30",
  159. "y": "110.2"
  160. },
  161. {
  162. "x": "2018-10-30 20:30",
  163. "y": "111.8"
  164. }
  165. ]
  166. },
  167. {
  168. "name": "User 2",
  169. "id": 1,
  170. "data": [
  171. {
  172. "x": "2018-10-27 20:30",
  173. "y": "103.5"
  174. },
  175. {
  176. "x": "2018-10-28 20:30",
  177. "y": "105.0"
  178. },
  179. {
  180. "x": "2018-10-29 20:30",
  181. "y": "105.5"
  182. },
  183. {
  184. "x": "2018-10-30 20:30",
  185. "y": "104.0"
  186. }
  187. ]
  188. }
  189. ]
  190. }
  191. EOF;
  192. } else {
  193. $sql = 'SELECT id, name FROM weight_users ORDER BY id ASC';
  194. $result = mysql_query($sql);
  195. if (!$result) {
  196. print_error('Error fetching users from database!');
  197. exit(1);
  198. }
  199. $data = array();
  200. while ($row = mysql_fetch_array($result)) {
  201. $sql2 = 'SELECT date, weight FROM weight_data ';
  202. $sql2 .= 'WHERE id = "' . $row['id'] . '" ORDER BY date ASC';
  203. $result2 = mysql_query($sql2);
  204. if (!$result2) {
  205. print_error('Error fetching data for user ' . $row['id'] . ' "' . $row['name'] . '"!');
  206. exit(1);
  207. }
  208. $cur = array();
  209. $cur['name'] = $row['name'];
  210. $cur['id'] = $row['id'];
  211. $cur['data'] = array();
  212. while ($row2 = mysql_fetch_array($result2)) {
  213. $elem = array();
  214. $elem['date'] = $row2['date'];
  215. $elem['weight'] = $row2['weight'];
  216. $cur['data'][] = $elem;
  217. }
  218. $data[] = $cur;
  219. }
  220. echo '{"users": [';
  221. foreach ($data as $data_key => $data_value) {
  222. if ($data_key > 0) {
  223. echo ',';
  224. }
  225. echo '{';
  226. echo '"name": "' . $data_value['name'] . '",';
  227. echo '"id": ' . $data_value['id'] . ',';
  228. echo '"data": [';
  229. foreach ($data_value['data'] as $row_key => $row_value) {
  230. if ($row_key > 0) {
  231. echo ',';
  232. }
  233. echo '{';
  234. echo '"x": "' . $row_value['date'] . '",';
  235. echo '"y": "' . $row_value['weight'] . '"';
  236. echo '}';
  237. }
  238. echo ']}';
  239. }
  240. echo ']}';
  241. }
  242. ?>