Finding the next times in a schedule table where stops are columns and trips are rows
I'm new to PHP, mysql etc. I've done some work in VB and Java, and am reasonably fluent in html.
I'm trying to set up a PHP webpage that looks up train times from a schedule that follows the form:
---------------------------------------开发者_Python百科------------------
| runId | stop1 | stop2 | stop3 | stop4 | stop5 | stop6 |
---------------------------------------------------------
| 1 | 5:00 | 5:10 | 5:21 | 5:34 | 5:40 | 6:00 |
---------------------------------------------------------
| 2 | 5:30 | 5:40 | 5:51 | 6:04 | 6:10 | 6:30 |
---------------------------------------------------------
| 3 | 6:00 | 6:10 | 6:21 | 6:34 | 6:40 | 7:00 |
---------------------------------------------------------
I then want to query the table knowing the current time and the stop number (column title) for the two entries that occur after the present time. So for example if I was at stop3 and it was 5:00 I would want the query to return $time[1]=5:21, $time[2]=5:51
but I would not want any more results to be returned (e.g. there should be no $time[3] = 6:21
set).
Any help would be greatly appreciated, this is a relatively small project, if it makes any difference to how you would recommend setting up the table.
The table should be like this:
runId stop time
1 1 5:00
1 2 5:10
1 3 5:21
...
2 1 5:30
2 2 5:40
...
create table schedule (runId int, stop int, `time` time);
insert into schedule (runId, stop, `time`) values
(1,1,'05:00'),
(1,2,'05:10'),
(1,3,'05:21'),
(1,4,'05:34'),
(1,5,'05:40'),
(1,6,'06:00'),
(2,1,'05:30'),
(2,2,'05:40'),
(2,3,'05:51'),
(2,4,'06:04'),
(2,5,'06:10'),
(2,6,'06:30'),
(3,1,'06:00'),
(3,2,'06:10'),
(3,3,'06:21'),
(3,4,'06:34'),
(3,5,'06:40'),
(3,6,'07:00')
;
And the query:
sql = "
select runId, stop, `time`
from schedule
where stop = $stop and `time` > current_time
limit 2
;
";
精彩评论