I have a Table "dtr", which contains 6 columns namely Name, ACNo, Date, Time, State and Exception.
Name | ACNo. | Date | Time | State | Exception|
-------------+-------+-----------+--------------+------------+----------+
Johnny Starks| 1220 | 5/13/2013 | 11:45:18 PM | Check In | OK |
Johnny Starks| 1220 | 5/14/2013 | 12:46:58 AM | Out | Out |
Johnny Starks| 1220 | 5/14/2013 | 12:52:41 AM | Out Back | Out |
Johnny Starks| 1220 | 5/14/2013 | 02:12:50 AM | Out | Out |
Johnny Starks| 1220 | 5/14/2013 | 02:43:11 AM | Out Back | Out |
Johnny Starks| 1220 | 5/14/2013 | 05:46:58 AM | Out | Out |
Johnny Starks| 1220 | 5/14/2013 | 06:22:41 AM | Out Back | Out |
Johnny Starks| 1220 | 5/14/2013 | 06:55:12 AM | Check Out | OK |
Johnny Starks| 1220 | 5/14/2013 | 11:47:13 PM | Check In | OK |
Johnny Starks| 1220 | 5/15/2013 | 12:36:28 AM | Out | Out |
Johnny Starks| 1220 | 5/15/2013 | 12:59:11 AM | Out Back | Out |
Johnny Starks| 1220 | 5/15/2013 | 03:12:54 AM | Out | Out |
Johnny Starks| 1220 | 5/15/2013 | 03:33:31 AM | Out Back | Out |
Johnny Starks| 1220 | 5/15/2013 | 06:55:12 AM | Check Out | OK |
So, What i want to get as a result using the codes below is... or the sequence i want the loop to do..
in the 1st loop - it will get 1 Check In then moves to the 2nd loop - will get the Check Out after getting the Check In and Check Out.. it will now go to the 3rd/last loop - which will be the one who will get the Breaks(the one with the Out Exception) using the range of the result of the 1st and 2nd loop.
I am doing this so I can calculate every pair of Out and Outbreak in every one(1) shift(ranging from Check-In to Check-Out). And after that, so I can calculate the total minutes/hour of the break for one(1) shift.
I cannot just based the range of the shift through Date. becoz if i do, the other breaks will be on the other/next shift which has the same Check-In date as the Break Date and I cannot calculate it properly or count the every pair of break it has.
but it seems i cannot get it right. What should i do in order to have the right result? or get the loop right...
<?php
include 'connection.php';
$checktime='';
$outtime = '';
$isql = mysql_query("Select * from dtr where ACNo = '1220' and
State = 'Check In'") or die(mysql_error());
while($irow = mysql_fetch_array($isql)){
$iID = $irow['ACNo'];
$iDate = $irow['Date'];
$iTime = $irow['Time'];
$iState = $irow['State'];
if($iState == 'Check In'){
$checktime = $iTime;
$indate = $iDate;
}
$osql = mysql_query("Select * from dtr where ACNo = '1220' and
State = 'Check Out'") or die(mysql_error());
while($orow = mysql_fetch_array($osql)){
$oID = $orow['ACNo'];
$oDate = $orow['Date'];
$oTime = $orow['Time'];
$oState = $orow['State'];
if($oState == 'Check Out'){
$outtime = $oTime;
$outdate = $oDate;
}
$bsql = mysql_query("select * from dtr where Exception = 'Out' And Time Between
'$checktime' and '$outtime'")or die(mysql_error());
while($brow = mysql_fetch_array($bsql)){
$bID = $brow['ACNo'];
$bDate = $brow['Date'];
$bTime = $brow['Time'];
$bState = $brow['State'];
echo $bDate.' - ';
echo $bTime.' ';
echo $bState.'<br>';
}
}
}
?>
edited: as requested.. the output that i've been expecting or wanting is like this...
5/13/2013 | 11:45:18 PM | Check In
5/14/2013 | 12:46:58 AM | Out
5/14/2013 | 12:52:41 AM | Out Back
5/14/2013 | 02:12:50 AM | Out
5/14/2013 | 02:43:11 AM | Out Back
5/14/2013 | 05:46:58 AM | Out
5/14/2013 | 06:22:41 AM | Out Back
5/14/2013 | 06:55:12 AM | Check Out
Break Count : 3
Total Mins Of Break: [total here]
5/14/2013 | 11:47:13 PM | Check In
5/15/2013 | 12:36:28 AM | Out
5/15/2013 | 12:59:11 AM | Out Back
5/15/2013 | 03:12:54 AM | Out
5/15/2013 | 03:33:31 AM | Out Back
5/15/2013 | 06:55:12 AM | Check Out
Break Count : 2
Total Mins Of Break: [total here]