forked from Persnickety/septa_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_schedule_info.php
62 lines (56 loc) · 2.31 KB
/
json_schedule_info.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?
if ($_GET && array_key_exists('service_id', $_GET)){
header('Content-type: application/json');
require_once( 'libs/lib.php');
connectDB();
send_data(mysql_real_escape_string($_GET['service_id']));
} else{
?>
<h2> Select a service</h2>
<form method="GET">
<select name='service_id'>
<option value='S1'>Weekdays</option>
<option value='S2'>Saturday</option>
<option value='S3'>Sunday/Holiday</option>
<option value='S4'>Friday (late train stuff)</option>
</select>
<input type = "submit" />
</form>
<?
}
exit;
function send_data($service_id){
// print "OK $service_id <br>\n";
$out = array();
$trips_q = mysql_query("SELECT block_id
FROM trips
WHERE service_id='$service_id'
GROUP BY block_id
ORDER BY block_id ASC;"
);
$n = 0;
while ($trip = mysql_fetch_object($trips_q)){
$b_id = 1*$trip->block_id;
$out[$n] = array();
$out[$n]['block_id'] = $b_id;
$out[$n]['schedule'] = array();
$q = "SELECT stop_id, FLOOR((TIME_TO_SEC( LEFT( departure_time,6) )-10800)/60) AS mins_since_3am
FROM stop_times
LEFT JOIN trips USING (block_id)
WHERE trips.block_id='$b_id' AND trips.service_id='$service_id'
ORDER BY stop_sequence ASC";
$stop_time_q = mysql_query($q);
$has_reached_suburban = false;
$last_stop_id=null;
while ($stop = mysql_fetch_object($stop_time_q)){
if($stop->stop_id == $last_stop_id) continue; //duplicates may exist at the overlap when changing trips within a block
$has_reached_suburban = $has_reached_suburban || ($stop->stop_id == '90005');
$out[$n]['schedule'][]= array($stop->stop_id, 1*$stop->mins_since_3am, $has_reached_suburban?1:0);
$last_stop_id = $stop->stop_id;
} //loop every stop
// if(!$has_reached_suburban) print "<span style='color:red'>$b_id</span> $q <br>".json_encode($out[$b_id])."<br>\n";
// else print "$b_id ok<br>\n";
$n++;
} //loop every trip
print json_encode($out);
}