/setupDatabase.php

https://github.com/idiosyncraticee/ranaeschallenge · PHP · 250 lines · 177 code · 55 blank · 18 comment · 45 complexity · 3648ad955c8c4e8eacca1536e6e194ec MD5 · raw file

  1. <?php
  2. header("Content-Type: text/javascript");
  3. header("Cache-Control: no-cache, must-revalidate");
  4. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // A date in the past
  5. $json = array();
  6. //$con = mysql_connect("ranae.db.6080705.hostedresource.com","ranae","Milton0");
  7. $con = mysql_connect("localhost","root","");
  8. if (!$con) {
  9. $json['db_connect_status'] = 'Could not connect: ' . mysql_error();
  10. $json['replyText'] = $json['db_connect_status'];
  11. $json['status'] = -1;
  12. $json['replyCode'] = 400;
  13. $encoded = json_encode($json);
  14. echo($encoded);
  15. return;
  16. }
  17. mysql_select_db("ranae", $con); //NAME OF THE DATABASE TO USE GOES HERE
  18. $production = 0;
  19. $development = 1;
  20. if($production) {
  21. ini_set('display_errors', 0);
  22. ini_set('error_reporting', 'E_ALL');
  23. ini_set('log_errors', 1);
  24. ini_set('error_log', './chris_error.txt');
  25. } else if($development) {
  26. ini_set('display_errors', 1);
  27. ini_set('error_reporting', 'E_ALL');
  28. ini_set('log_errors', 1);
  29. ini_set('error_log', './chris_error.txt');
  30. }
  31. ///////////////////////////////////////////////////////////////
  32. /////////////// FUNCTIONS BELOW HERE /////////////////////////
  33. ///////////////////////////////////////////////////////////////
  34. function username2userid($username) {
  35. $sql = "SELECT id FROM users WHERE username='{$username}'";
  36. $result = mysql_query($sql);
  37. if($result) {
  38. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  39. if($row['id']){
  40. $userid = $row['id'];
  41. } else {
  42. $json['replyText'] = "NOTHING RETURNED FROM row ARRAY";
  43. $json['replyCode'] = 522;
  44. }
  45. } else if(mysql_error()) {
  46. $json['replyText'] = mysql_error().":".$userid.":". $sql;
  47. $json['replyCode'] = 523;
  48. } else {
  49. $json['replyCode'] = 524;
  50. }
  51. return ($userid);
  52. }
  53. function getTableId($getid,$column,$userid,$newValue,$con) {
  54. if(strcmp($getid,'undefined')==0) { //NO ID IS DEFINED THIS IS A NEW ROW
  55. //CREATE A NEW ROW AND GIVE IT THE INFORMATIONS THAT IS PROVIDED
  56. $sql_query = "INSERT INTO journey ( userid, {$column}) VALUES ('{$userid}','{$newValue}')";
  57. $result = mysql_query($sql_query);
  58. $DBid = mysql_insert_id();
  59. } else { // ID IS ALREADY DEFINED
  60. $DBid = mysql_real_escape_string($getid, $con);
  61. }
  62. return ($DBid);
  63. }
  64. function getStartWeight($userid) {
  65. //GET THE START WEIGHT
  66. $sql = "SELECT weight FROM journey WHERE userid = {$userid} AND weight != '' AND timestamp = (select min(timestamp) from journey where userid = {$userid} and weight != '')";
  67. $result = mysql_query($sql);
  68. if(mysql_error()) {
  69. $json['replyText'] = mysql_error().":$userid:". $sql;
  70. $json['replyCode'] = 505;
  71. $encoded = json_encode($json);
  72. echo($encoded);
  73. return;
  74. }
  75. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  76. if($row['weight']) { // THERE ARE SOME ENTRIES ALREADY
  77. $startWeight = $row['weight'];
  78. } else {
  79. //NO DATA YET FOR THIS USER
  80. $startWeight = $newValue;
  81. }
  82. return ($startWeight);
  83. }
  84. function getLastWeight($userid) {
  85. //GET THE LAST WEIGHT
  86. $sql = "SELECT weight FROM journey WHERE userid = {$userid} AND weight != '' and timestamp = (select max(timestamp) from journey where userid = {$userid} and weight != '')";
  87. $result = mysql_query($sql);
  88. $rowCount = mysql_num_rows($result);
  89. if(mysql_error()) {
  90. $json['replyText'] = mysql_error().":{$userid}:". $sql;
  91. $json['replyCode'] = 506;
  92. $encoded = json_encode($json);
  93. echo($encoded);
  94. return;
  95. }
  96. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  97. if($row['weight']) { // THERE ARE SOME ENTRIES ALREADY
  98. $lastWeight = $row['weight'];
  99. } else {
  100. //NO DATA YET FOR THIS USER
  101. $lastWeight = $newValue;
  102. }
  103. if(!isset($lastWeight) && $rowCount>0) {
  104. $json['replyText'] = "ERROR: lastWeight is undefined:".$lastWeight;
  105. $json['replyCode'] = 504;
  106. $encoded = json_encode($json);
  107. echo($encoded);
  108. return;
  109. }
  110. return ($lastWeight);
  111. }
  112. function getTimestampFromId($userid,$DBid) {
  113. $sql = "SELECT timestamp FROM journey WHERE id = {$DBid} AND userid = {$userid}";
  114. $result = mysql_query($sql);
  115. if(mysql_error()) {
  116. $json['replyText'] = mysql_error()."::". $sql;
  117. $json['replyCode'] = 513;
  118. $encoded = json_encode($json);
  119. echo($encoded);
  120. return;
  121. }
  122. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  123. return($row['timestamp']);
  124. }
  125. function getStartDate($userid) {
  126. $sql = "SELECT min(j.timestamp) FROM journey AS j, users AS u WHERE j.userid = u.id AND u.id='{$userid}' AND weight != ''";
  127. $result = mysql_query($sql);
  128. if($result) {
  129. while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
  130. $startDate = $row['min(j.timestamp)'];
  131. }
  132. } else if(mysql_error()) {
  133. $json['replyText'] = mysql_error().":".$userid.":". $sql;
  134. $json['replyCode'] = 506;
  135. } else {
  136. $json['replyCode'] = 507;
  137. }
  138. return($startDate);
  139. }
  140. function getWeightFromId($userid,$DBid) {
  141. $sql = "SELECT weight FROM journey WHERE id = {$DBid}";
  142. $result = mysql_query($sql);
  143. if(mysql_error()) {
  144. $json['replyText'] = mysql_error()."::". $sql;
  145. $json['replyCode'] = 513;
  146. $encoded = json_encode($json);
  147. echo($encoded);
  148. return;
  149. }
  150. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  151. return($row['weight']);
  152. }
  153. function getFirstMonthWeight($userid,$timestamp) {
  154. //TODO: MAKE THIS WORK
  155. /////////////////////////////////////////////////////////////////
  156. //GET THE MONTH WEIGHT
  157. $sql = "SELECT weight,timestamp FROM journey WHERE userid = {$userid} AND timestamp != '' AND weight != '' ORDER BY timestamp ASC";
  158. $result = mysql_query($sql);
  159. //$rowCount = mysql_num_rows($result);
  160. if(mysql_error()) {
  161. $json['replyText'] = mysql_error().":{$userid}:". $sql;
  162. $json['replyCode'] = 506;
  163. $encoded = json_encode($json);
  164. echo($encoded);
  165. return;
  166. }
  167. $arr1 = str_split($timestamp);
  168. $thisMonth = $arr1[4].$arr1[5];
  169. //TODO: GET THIS WORKING
  170. $lastMonth = 0;
  171. while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
  172. $arr1 = str_split($row['timestamp']);
  173. $row['timestamp'] = $arr1[4].$arr1[5]."/".$arr1[6].$arr1[7]."/".$arr1[0].$arr1[1].$arr1[2].$arr1[3];
  174. $month = $arr1[4].$arr1[5];
  175. // TODO: THIS DOES THE MONTHLY CALCS
  176. if( $lastMonth == 0 ) { //FIRST ENTRY IN THE WHOLE THING REPRESENTS THE FIRST MONTH
  177. $firstMonth = $month;
  178. //IF THE ENTRY IS FOR THE FIRST MONTH THE FIRST ENTRY IS THE FIRST ENTRY OF THIS MONTH
  179. if($firstMonth == $thisMonth) {
  180. $firstWeightThisMonth = $row['weight'];
  181. return($firstWeightThisMonth);
  182. }
  183. } else if( $month != $lastMonth) { //FIRST ENTRY OF THE MONTH BEGINS A NEW MONTH
  184. //GET THE LAST ENTRY FOR THE PREVIOUS MONTH
  185. if($month == $thisMonth) {
  186. $firstWeightThisMonth = $lastWeight;
  187. return($firstWeightThisMonth);
  188. }
  189. }
  190. //print_r("{$row['timestamp']} {$row['weight']} {$perLostThisMonth}\n");
  191. $lastMonth = $month;
  192. $lastWeight = $row['weight'];
  193. }
  194. }
  195. ?>