Hi,
I am finding very difficult to store the previous row data.
Actually records in database is like this
Name| Timestamp | Status
A | 2008-06-08 05:00:01 | 0
A | 2008-06-08 05:10:01 | 0
A | 2008-06-08 05:20:01 | 0
A | 2008-06-08 05:30:01 | 0
A | 2008-06-08 05:40:01 | 0
A | 2008-06-08 05:50:01 | 0
A | 2008-06-08 06:00:02 | 0
A | 2008-06-08 06:10:01 | 0
A | 2008-06-08 06:20:01 | 1
A | 2008-06-08 06:30:01 | 1
A | 2008-06-08 06:40:01 | 1
A | 2008-06-08 06:50:01 | 0
A | 2008-06-08 07:00:01 | 0
A | 2008-06-08 07:10:01 | 0
A | 2008-06-08 07:20:01 | 0
A | 2008-06-08 07:30:01 | 0
A | 2008-06-08 07:40:01 | 0
A | 2008-06-08 07:50:01 | 0
A | 2008-06-08 08:00:01 | 0
A | 2008-06-08 08:10:01 | 1
A | 2008-06-08 08:20:01 | 1
A | 2008-06-08 08:30:01 | 1
A | 2008-06-08 08:40:01 | 1
A | 2008-06-08 08:50:02 | 0
A | 2008-06-08 09:00:02 | 0
Problem is want to find the difference of only status when it is 0 to 1.
Suppose if timestamp is 0 at 2008-06-08 05:00:01 to 2008-06-08 06:10:01 I have to find the difference and again from 2008-06-08 06:50:01 to 2008-06-08 08:00:01 and so on.
Please help me on this.
I used the code
$row=mysql_query("select * from tablename where date(timestamp)=date(now()");
$previousStatus = '';
$currentStatus = '';
$previoustime='';
$currentTime='';
while($result = mysql_fetch_array($row))
{
$currentStatus = $result['status'];
$currentTime = $result['timestamp'];
$previousStatus = $previousStatus == '' ? $currentStatus : $previousStatus;
$previoustime = $previoustime == '' ? $currentTime : $previoustime;
if($previousStatus == $currentStatus)
{
$previousStatus=$currentStatus;
continue;
}
$d = strtotime($currentTime) - strtotime($previoustime);
array_push($records,"$result[2]#$result[2]#$previoustime##d");
$previousStatus = $currentStatus;
$previoustime=$currentTime;
The prblem is if status is 0 then it comes ou of the loop and previoustime is not stored properly, the data is pushed into the array when status changes from 1 to 0.
When data is pushed into the array records, the previoustime will be currenttime of previous row.