Edit:
Summarised Question:
Database has 1 table with 70 columns and 20 rows.
The last 63 columns are all int(4) and named bus-time01 to bus-time63.
Min value for data in these columns is 0. Max value is 2359.
Any values greater than 0 will be a minumum value of 500.
Column values all increment upwards in each row (e.g. 0501, 0539, 1149, 2138).
Never downwards and never identical numbers in same row (but other rows may contain identical values).
I run a select query that reduces rows down to between 1 and 4.
I wish to add to my select query some code to search with an integer, between 500-2359, for a match. Without specifying each column name if possible (as query would become huge)
I wish to find closest match to that integer in each row.
If no exact match, then the next highest number.
Once a match is found I would like the value of the column to the left and right of the one found.
I do not require the column names returned (if this is easier), just the returned values.
Can I do this with a query?
Detailed question:
I have a simple Javascript/PHP/MySQL/Ajax web application that displays bus times.
I want to improve on it so it shows just the next bus due (as well as the earlier bus and the next later bus).
Bus times are stored in database as:
columns:
bus-time01
bus-time02
bus-time03
And so on up to bus-time63.
int(4) Time values are stored as: 202, 930, 1230, 1945 etc
The database has just the one table, with just 20 rows (so not large at all).
70 columns in total. The other columns are: id, bus_number, stop_id, start_stop, end_stop, special, day.
My query as it is returns anything from 1 to 4 rows which are stored in a table. The variables for the query come from select boxes and divs on the page. I use ajax and a separate php page to run the query:
$result=mysqli_query($conn, "SELECT * FROM `timetable` WHERE `start_stop` = '" . $ss . "' AND `end_stop` = '" . $es . "' AND `day` = '" . $tmpDay . "'");
What I want is for each row displayed in the table to only show times close to the one in the page div (which displays the eact time). Explained more below.
I have a javascript function that gets the current time. It is displayed in web pages, as with the database values, as. e.g. 0202, 0930, 1230, 1945 etc.
The only difference is it shows the leading zero (which may casue an issue?).
This is the time function:
(function() {
function checkTime(i) {
return (i < 10) ? "0" + i : i;
}
function getTimeNow() {
var today = new Date(),
hournow = checkTime(today.getHours()),
minutesnow = checkTime(today.getMinutes()),
timenow = hournow + "" + minutesnow;
document.getElementById('time-now').innerHTML = timenow;
t = setTimeout(function() {
getTimeNow()
}, 500);
}
getTimeNow();
})();
What I want to achieve is to get the current time from the time-now field, query the database, and display only the records that match the criteria below.
e.g. If time is 0913 then get the records that are closest to that, for each valid row. Where one is lower and 2 are higher (or possibly 1 of these is equal and 1 is higher). i.e. 910, 915 and 920 (or 910, 913 and 920).
The table will look like the below:
If the time is currently 09:13
Bus# | Stop# | Earlier Bus | Next Bus | Later Bus |
---------------------------------------------------
1 | Z | 0910 | 0915 | 0920 |
5 | B | 0905 | 0920 | 0935 |
3 | A | 0900 | 0930 | 1000 |
This is all new to me and I really don't know where to begin here. I guess I need to get the whole row of times and somehow to search them for the values I require? Or is there a way to ask SQL to directly fetch the records I need.
Thanks for any pointers in the right direction.