What I want to do is set up a page that can monitor the availability of a piece of plant, i.e. how many hours in a day was a JCB working? I'd like to have a single button that will change the status of the plant (easy) on a page but I'd like to track when the status changed and then be able to calculate the availability for a given period (the bit I'm stuck on).
I'm struggling to think of how I can structure the database and php script to allow me to do this... Any ideas/help greatly appreciated :)
You could structure your table with columns for JCB ID, start time, and end time like below
jcb_id start_time stop_time
1 2013-01-31 00:00:00 2013-01-31 06:19:00
1 2013-01-31 21:04:00 null
2 2013-01-31 08:00:45 2013-01-31 18:19:00
In the example above:
jcb_id
1 is currently active because the stop_time
is null.jcb_id
2 is down because there is no record with a null
stop time.When you click the button, the page will check to see if there is a record for the JCB with a null
stop_time
.
jcb_id = ?
and stop_time is null
null
stop time exists for the JCB,NOW()
- JCB is not workingnull
stop time record for the JCBstop_time
null
- JCB is workingFrom there, you can run a query to calculate the time between the starts and stops to get up/downtime.
This isn't a fix to your problem, just some ideas that might help you find a solution yourself.
First of all, grab a pen(cil) and a piece of paper, and start writing down ideas, how you might do this and that, database design, and whatever you think is relevant. After a bit, you will have an idea on how to start.
Start with the idea you came up even if you're not convinced it's the best, and may have some flaws (like performance). After a few hours, you will possibly have to start from scratch, but you will know exactly the issues you encountered previously, and probably have a good idea on what you were missing / what you did right.
That's about it. Trial and error, that's the way programming works, and doing so you will become a better and better programmer.
PS: If you want accurate, code examples, go ahead and start writing code, and post questions that are related to code.