PageRenderTime 36ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/private_html/home/develop/ver1.2/php/test.php

https://github.com/UniBus/Server
PHP | 282 lines | 227 code | 47 blank | 8 comment | 20 complexity | 1d95875848e8e02d392810b3c4a3ea8f MD5 | raw file
  1. <?php
  2. function openDB($dbServer, $dbUser, $dbPass, $dbName)
  3. {
  4. $link = mysql_connect("$dbServer", "$dbUser", "$dbPass") or die("Failed: Could not connect");
  5. mysql_select_db("$dbName") or die("Failed: Could not select database: ".$dbName);
  6. return $link;
  7. }
  8. function closeDB($link)
  9. {
  10. mysql_close($link);
  11. }
  12. function queryCityAgency($city)
  13. {
  14. $cityDBServer='localhost';
  15. $dbUser='root';
  16. $dbPass='youneedpasswod';
  17. $dbName='bus_'.$city;
  18. $cityDBLink = openDB($cityDBServer, $cityDBUser, $cityDBPass, $cityDBName);
  19. $query = 'SELECT agency_name FROM agency';
  20. $result = mysql_query($query, $cityDBLink) or die('Query failed: '. mysql_error());
  21. $totalRows= mysql_num_rows($result);
  22. $agencyNames = '';
  23. if ($totalRows > 0) { // Show if recordset not empty
  24. $currentRow = mysql_fetch_assoc($result);
  25. do {
  26. if ($agencyNames == ''){
  27. $agencyNames = $currentRow['agency_name'];
  28. }
  29. else{
  30. $agencyNames = $agencyNames.'<br>'.$currentRow['agency_name'];
  31. }
  32. } while ($currentRow = mysql_fetch_assoc($result));
  33. } // Show if recordset not empty
  34. mysql_free_result($result);
  35. //closeDB($cityDBLink);
  36. return $agencyNames;
  37. }
  38. function GetDeltaTime($dtTime1, $dtTime2)
  39. {
  40. $nUXDate1 = strptime($dtTime1, "Ymd");
  41. $nUXDate2 = strptime($dtTime2, "Ymd");
  42. $nUXDelta = $nUXDate1 - $nUXDate2;
  43. $dDeltaTime = "" . $nUXDelta/60/60/24; // sec -> day
  44. return $dDeltaTime;
  45. }
  46. function queryCityExpirary($city)
  47. {
  48. $cityDBServer='localhost';
  49. $dbUser='root';
  50. $dbPass='youneedpasswod';
  51. $dbName='bus_'.$city;
  52. $cityDBLink = openDB($cityDBServer, $cityDBUser, $cityDBPass, $cityDBName);
  53. $query = 'SELECT service_id,start_date,end_date FROM calendar ORDER BY end_date DESC';
  54. $result = mysql_query($query, $cityDBLink) or die('Query failed: '. mysql_error());
  55. $totalRows= mysql_num_rows($result);
  56. $expiredDate = '';
  57. if ($totalRows > 0) { // Show if recordset not empty
  58. $currentRow = mysql_fetch_assoc($result);
  59. $expiredDate = $currentRow['end_date'];
  60. } // Show if recordset not empty
  61. mysql_free_result($result);
  62. if ($expiredDate != ''){
  63. return $expiredDate;
  64. }
  65. $query = 'SELECT service_id, date FROM calendar_dates ORDER BY date DESC';
  66. $result = mysql_query($query, $cityDBLink) or die('Query failed: '. mysql_error());
  67. $totalRows= mysql_num_rows($result);
  68. if ($totalRows > 0) { // Show if recordset not empty
  69. $currentRow = mysql_fetch_assoc($result);
  70. $expiredDate = $currentRow['date'];
  71. } // Show if recordset not empty
  72. mysql_free_result($result);
  73. //closeDB($cityDBLink);
  74. return $expiredDate;
  75. }
  76. function listAllCities()
  77. {
  78. $dbServer='localhost';
  79. $dbUser='root';
  80. $dbPass='youneedpasswod';
  81. $dbName='gtfs_info_v12';
  82. $link = openDB($dbServer, $dbUser, $dbPass, $dbName);
  83. $query = 'SELECT * FROM cities ORDER BY country, state, name';
  84. $result = mysql_query($query, $link) or die('Query failed: '. mysql_error());
  85. $totalRows= mysql_num_rows($result);
  86. $citiesFound = array();
  87. if ($totalRows > 0) { // Show if recordset not empty
  88. $currentRow = mysql_fetch_assoc($result);
  89. do {
  90. //echo('<td>'); echo $currentRow['id']; echo('</td>');
  91. $cityId = $currentRow['id'];
  92. $cityName = $currentRow['name'].', '. $currentRow['state'].', '. $currentRow['country'];
  93. $citiesFound[$cityName] = $cityId;
  94. } while ($currentRow = mysql_fetch_assoc($result));
  95. } // Show if recordset not empty
  96. //Free resultset
  97. mysql_free_result($result);
  98. return $citiesFound;
  99. }
  100. function getRandomStop($link)
  101. {
  102. $query = 'SELECT * FROM stops ORDER BY RAND() LIMIT 1';
  103. $result = mysql_query($query, $link) or die('Query failed: '. mysql_error());
  104. $totalRows= mysql_num_rows($result);
  105. $stopFound = array();
  106. if ($totalRows > 0) { // Show if recordset not empty
  107. $currentRow = mysql_fetch_assoc($result);
  108. $stopFound['stop_id'] = $currentRow['stop_id'];
  109. $stopFound['stop_name'] = $currentRow['stop_name'];
  110. $stopFound['stop_lon'] = $currentRow['stop_lon'];
  111. $stopFound['stop_lat'] = $currentRow['stop_lat'];
  112. } // Show if recordset not empty
  113. //Free resultset
  114. mysql_free_result($result);
  115. return $stopFound;
  116. }
  117. function getRandomRoute($link)
  118. {
  119. $query = 'SELECT route_id, route_short_name route_long_name FROM routes ORDER BY RAND() LIMIT 1';
  120. $result = mysql_query($query, $link) or die('Query failed: '. mysql_error());
  121. $totalRows= mysql_num_rows($result);
  122. $routeFound = array();
  123. if ($totalRows > 0) { // Show if recordset not empty
  124. $currentRow = mysql_fetch_assoc($result);
  125. $routeFound['route_id'] = $currentRow['route_id'];
  126. $routeFound['route_short_name'] = $currentRow['route_short_name'];
  127. $routeFound['route_long_name'] = $currentRow['route_long_name'];
  128. } // Show if recordset not empty
  129. //Free resultset
  130. mysql_free_result($result);
  131. return $routeFound;
  132. }
  133. function getRandomTrip($link)
  134. {
  135. $query = 'SELECT trip_id, trip_headsign, route_id, direction_id FROM trips ORDER BY RAND() LIMIT 1';
  136. $result = mysql_query($query, $link) or die('Query failed: '. mysql_error());
  137. $totalRows= mysql_num_rows($result);
  138. $tripFound = array();
  139. if ($totalRows > 0) { // Show if recordset not empty
  140. $currentRow = mysql_fetch_assoc($result);
  141. $tripFound['trip_id'] = $currentRow['trip_id'];
  142. $tripFound['route_id'] = $currentRow['route_id'];
  143. $tripFound['direction_id'] = $currentRow['direction_id'];
  144. $tripFound['trip_headsign'] = $currentRow['trip_headsign'];
  145. if ($tripFound['trip_headsign'] == '')
  146. $tripFound['trip_headsign'] = '(No Headsign)';
  147. } // Show if recordset not empty
  148. //Free resultset
  149. mysql_free_result($result);
  150. return $tripFound;
  151. }
  152. function getRandomTrip2($link)
  153. {
  154. $randomTrip = getRandomTrip($link);
  155. $query = 'SELECT stop_id FROM stop_times WHERE trip_id="'. $randomTrip['trip_id']. '" ORDER BY RAND() LIMIT 1';
  156. $result = mysql_query($query, $link) or die('Query failed: '. mysql_error());
  157. $totalRows= mysql_num_rows($result);
  158. $tripFound = array();
  159. if ($totalRows > 0) { // Show if recordset not empty
  160. $currentRow = mysql_fetch_assoc($result);
  161. $tripFound['trip_id'] = $randomTrip['trip_id'];
  162. $tripFound['route_id'] = $randomTrip['route_id'];
  163. $tripFound['direction_id'] = $randomTrip['direction_id'];
  164. $tripFound['stop_id'] = $currentRow['stop_id'];
  165. $tripFound['trip_headsign'] = $randomTrip['trip_headsign'];
  166. } // Show if recordset not empty
  167. //Free resultset
  168. mysql_free_result($result);
  169. return $tripFound;
  170. }
  171. function writeRandomTestHTMLStop($city, $items)
  172. {
  173. $dbServer='localhost';
  174. $dbUser='root';
  175. $dbPass='youneedpasswod';
  176. $dbName='bus_'.$city;
  177. $link = openDB($dbServer, $dbUser, $dbPass, $dbName);
  178. echo '<ul>';
  179. for ($i=1; $i<=5; $i++) {
  180. $randomStop = getRandomStop($link);
  181. echo '<li><a href=http://unibus.ca:5144/ver1.2/'.$city.'/arrivals.php?stop_id='.$randomStop['stop_id'].'>'.$randomStop['stop_name'].'</a>'.
  182. ' [<a href=http://unibus.ca:5144/ver1.2/'.$city.'/stopmap.php?lat='.$randomStop['stop_lat'].'&long='.$randomStop['stop_lon'].'>map</a>]'.
  183. '</li>';
  184. }
  185. echo '</ul>';
  186. closeDb($link);
  187. }
  188. function writeRandomTestHTMLSchedule($city, $items)
  189. {
  190. $dbServer='localhost';
  191. $dbUser='root';
  192. $dbPass='youneedpasswod';
  193. $dbName='bus_'.$city;
  194. $link = openDB($dbServer, $dbUser, $dbPass, $dbName);
  195. echo '<ul>';
  196. for ($i=1; $i<=5; $i++) {
  197. $randomTrip = getRandomTrip2($link);
  198. echo '<li><a href=http://unibus.ca:5144/ver1.2/'.$city.'/schedules.php?stop_id='.$randomTrip['stop_id'].
  199. '&route_id='.$randomTrip['route_id'].'&direction_id='.$randomTrip['direction_id'].
  200. '>'.$randomTrip['route_id'].'@'.$randomTrip['stop_id'].' ('. $randomTrip['trip_headsign'] .')' .'</a></li>';
  201. }
  202. echo '</ul>';
  203. closeDb($link);
  204. }
  205. function writeRandomTestHTMLRoute($city, $items)
  206. {
  207. $dbServer='localhost';
  208. $dbUser='root';
  209. $dbPass='youneedpasswod';
  210. $dbName='bus_'.$city;
  211. $link = openDB($dbServer, $dbUser, $dbPass, $dbName);
  212. echo '<ul>';
  213. for ($i=1; $i<=5; $i++) {
  214. $randomRoute = getRandomRoute($link);
  215. echo '<li><a href=http://unibus.ca:5144/ver1.2/'.$city.'/tripsofroute.php?route_id='.$randomRoute['route_id'].'>'.$randomRoute['route_short_name'].' (' . $randomRoute['route_long_name']. ')'.'</a></li>';
  216. }
  217. echo '</ul>';
  218. closeDb($link);
  219. }
  220. function writeRandomTestHTMLTrip($city, $items)
  221. {
  222. $dbServer='localhost';
  223. $dbUser='root';
  224. $dbPass='youneedpasswod';
  225. $dbName='bus_'.$city;
  226. $link = openDB($dbServer, $dbUser, $dbPass, $dbName);
  227. echo '<ul>';
  228. for ($i=1; $i<=5; $i++) {
  229. $randomTrip = getRandomTrip($link);
  230. echo '<li><a href=http://unibus.ca:5144/ver1.2/'.$city.'/routemap.php?trip_id='.$randomTrip['trip_id'].'>'.$randomTrip['route_id'].' (' . $randomRoute['trip_headsign']. ')'.'</a></li>';
  231. }
  232. echo '</ul>';
  233. closeDb($link);
  234. }
  235. ?>