Reputation: 681
i have a mysql table in the following format
-------------------------------------------------------------------------------------------------------------
| event_id | event_type | machine | user | timeStamp | Shift | Reason | Count |
-------------------------------------------------------------------------------------------------------------
| 101 | Up | Machine1 | operator1 | 2012-09-06 01:03:55 | Shift1 | Start of The shift | 1 |
| 102 | Up | Machine2 | operator2 | 2012-09-06 01:03:55 | Shift1 | Start of The shift | 1 |
| 103 | Up | Machine3 | operator3 | 2012-09-06 01:03:55 | Shift1 | Start of The shift | 1 |
| 104 | Down | Machine1 | operator1 | 2012-09-06 02:03:55 | Shift1 | Break Down | 1 |
| 101 | Up | Machine1 | operator1 | 2012-09-06 02:33:55 | Shift1 | After BD | 1 |
-------------------------------------------------------------------------------------------------------------
by passing the following query.
$data = mysql_query("select * from rpt_machine_log where (timestamp between date_sub(now(),INTERVAL 2 WEEK) and now() && (machine='machine$i') && (shift='Shift1') ) AND (reason='Start of The Shift' OR reason='End of Shift') ORDER BY 'even_id'")
i get the following output
-------------------------------------------------------------------------------------------------------------
| event_id | event_type | machine | user | timeStamp | Shift | Reason | Count |
-------------------------------------------------------------------------------------------------------------
| 101 | Up | Machine1 | operator1 | 2012-09-06 01:03:55 | Shift1 | Start of The shift | 1 |
| 118 | Down | Machine1 | operator1 | 2012-09-06 06:00:55 | Shift1 | End of The shift | 1 |
| 127 | Up | Machine1 | operator1 | 2012-09-07 01:03:55 | Shift1 | Start of The shift | 1 |
| 148 | Down | Machine1 | operator1 | 2012-09-07 02:03:55 | Shift1 | End of The shift | 1 |
-------------------------------------------------------------------------------------------------------------
I calculate Total Shift Time by finding the difference of the End of The shift and Star of The shift like the following code.
while ($row = mysql_fetch_assoc($shifttime{$i}))
{
if(isset($timestamp))
{
$diff_shift_time{$i}= strtotime($row['timestamp']) - strtotime($timestamp);
$diff_shift_time_array{$i}[]=$diff_shift_time{$i};
unset($timestamp);
}
else
{
$timestamp=$row['timestamp'];
}
}
foreach ($diff_shift_time_array{$i} as &$value){
$Total_shift_time{$i} += $value;
}
in some cases the output of the query is as follows.
-------------------------------------------------------------------------------------------------------------
| event_id | event_type | machine | user | timeStamp | Shift | Reason | Count |
-------------------------------------------------------------------------------------------------------------
| 121 | Up | Machine1 | operator1 | 2012-09-06 06:03:55 | Shift2 | Start of The shift | 1 |
| 124 | Down | Machine1 | operator1 | 2012-09-06 01:00:55 | Shift2 | End of The shift | 1 |
| 151 | Up | Machine1 | operator1 | 2012-09-07 06:03:55 | Shift2 | Start of The shift | 1 |
-------------------------------------------------------------------------------------------------------------
when i calculate the total shift time, i get negative value, because my resultant table ends with Up event.
Since the calculation is for consecutive up and down(so the no of rows of the resultant row should always be even
), the last up doesn't have down event to perform calculation.
I want to delete the last row of the table if the row count is odd.
How can i achieve it.
Upvotes: 1
Views: 83
Reputation: 418
try
$count = mysql_num_rows($shifttime{$i});
$remove_last = $count%2==0?false:true;
$cur_key = 0;
while ($row = mysql_fetch_assoc($shifttime{$i}))
{
if(TRUE === $remove_last && $cur_key == ($count-1))
break;
$cur_key++;
if(isset($timestamp))
{
$diff_shift_time{$i}= strtotime($row['timestamp']) - strtotime($timestamp);
$diff_shift_time_array{$i}[]=$diff_shift_time{$i};
unset($timestamp);
}
else
{
$timestamp=$row['timestamp'];
}
}
Upvotes: 2
Reputation: 1898
Check the size of the array to see if the size is odd:
if (sizeof($arr) % 2 == 1) {
// This array is odd sized.
array_pop($arr); // Remove last element.
}
Upvotes: 2