Forums
This topic is locked
Logic Problem
Posted 11 Feb 2008 19:28:18
1
has voted
11 Feb 2008 19:28:18 Seb Adlington posted:
Hi,Slightly off topic but I'm trying to get my head around this. I'm working from a database that
i have no control over but have read access to the data.
I have a recordset that lists activities that drivers have been doing and records the time,
but not the duration so:
Recordset for Fred
6:00 Driving
6:05 Rest
6:10 Driving
6:15 Parked
6:20 Driving
6:30 Rest
6:35 End Day
etc
I need to loop through and get the totals for each kind of activity.
ie.
Driving = 20 mins
Rest = 10 mins
Parked = 5 mins
Total Day = 35 mins
I've converted the times into minutes since midnight to help the maths. The total is easy enough
but not having fun with the individual times. I've tried doing something with variables and trying
to store the previous time etc but getting stuck in logic hell
Help
thanks
Seb
Replies
Replied 12 Feb 2008 10:19:25
12 Feb 2008 10:19:25 Alan C replied:
HI
interesting requirement - looks to me like the start time defines the type of activity, like driving, and the operator can only be doing one thing at once, so I don't think it will be too difficult to accumulate the totals.
Something like . . .
(This may look a bit like php <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle> my js is not brilliant)
Initialise an associative array of totals using the activity names as keys, all times =0
foreach line {
current activity = activity from line
start time = start time from line
get next line
calculate elapsed time
time total for this activity = time total + elapsed time
if activity = end day end loop
}
that pseudocode might not work but the idea is to loop through looking at what activity is taking place, work out the duration and add that to the array of totals, after the loop the array will contain one row for each activity and the total time spent on it, then you can total it using another foreach
I would see the end condition as the thing to watch so that you're not looking for an array element or whatever that is undefined
interesting requirement - looks to me like the start time defines the type of activity, like driving, and the operator can only be doing one thing at once, so I don't think it will be too difficult to accumulate the totals.
Something like . . .
(This may look a bit like php <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle> my js is not brilliant)
Initialise an associative array of totals using the activity names as keys, all times =0
foreach line {
current activity = activity from line
start time = start time from line
get next line
calculate elapsed time
time total for this activity = time total + elapsed time
if activity = end day end loop
}
that pseudocode might not work but the idea is to loop through looking at what activity is taking place, work out the duration and add that to the array of totals, after the loop the array will contain one row for each activity and the total time spent on it, then you can total it using another foreach
I would see the end condition as the thing to watch so that you're not looking for an array element or whatever that is undefined
Replied 15 Feb 2008 23:56:43
15 Feb 2008 23:56:43 Seb Adlington replied:
Hi Alan,
thanks for the response. I got it working eventually. You were right arrays were the way to go. I think
I initially had problems overwriting variables and needed to get the order of the code right!
thanks
Seb
thanks for the response. I got it working eventually. You were right arrays were the way to go. I think
I initially had problems overwriting variables and needed to get the order of the code right!
thanks
Seb