This is really hard to explain, but i will do my best.
I have a array called arrayoftransationcs which contains 4 strings. *SCAN, MEMBER_ID, RESTAURANT, TIME, PAID. If a scan is paid, it will defined as 1, and if not it will have 0.
So the main question to my problem is, how can I get scan that contain paid = 0 which has not have paid = 1 within 60 min. I will give an example.
Example A In this example, there is 3 scan which comes from the same cafe x within 20 min. The first 2 has been defined as paid = 0, and the last has been defined as paid = 1. Because there is paid = 1 within 60min with the same member_id and restaurant, i dont want any of the scan
Scan A 1635752 Cafe X 17-11-2013 21:00 Paid 1
Scan B 1635752 Cafe X 17-11-2013 20:50 Paid 0
Scan C 1635752 Cafe X 17-11-2013 20:40 Paid 0
Example B In this example, there is also 3 scan from the same member, same restaurant, within 60min, but all of them has been defined as paid = 0. Because there is no paid = 1 in this example, i want these in a array. This is the goal. But there is a twist here, because there is more than 1 scan to use, i only want the latest scan in this example, which will mean only scan A can be used
Scan A 1635752 Cafe X 17-11-2013 21:00 Paid 0
Scan B 1635752 Cafe X 17-11-2013 20:50 Paid 0
Scan C 1635752 Cafe X 17-11-2013 20:40 Paid 0
So I hope you understand the my question. I need paid = 0 scan if there is not a paid = 1 scan subsequently with the same member_id, restaurant within 60min
This is how i tried.
I am looping my array 2 times, and then checking for it the same member_id (cardid) and restaurants are equals together, if yes, then check time. If it is whitin 60min, mark the scan as double
foreach($arrayoftransationcs as $key => $array)
{
$time=$arrayoftransationcs[$key]['created'];
$cardid = $arrayoftransationcs[$key]['cardid'];
$restaurant_id = $arrayoftransationcs[$key]['restaurant_id'];
if(isset($arrayoftransationcs[$key]))
{
foreach($arrayoftransationcs as $k1=>$v1)
{
$time2=$arrayoftransationcs[$k1]['created'];
if($key<$k1)
{
if($arrayoftransationcs[$k1]['cardid']==$cardid && $arrayoftransationcs[$k1]['restaurant_id']==$restaurant_id)
{
if(compare($time,$time2))
{
$arrayoftransationcs[$key]['error'] = 'double';
$arrayoftransationcs[$k1]['error'] = 'double';
}
}
}
}
}
}
checking the time here.
function compare($firsttime, $secondtime)
{
$interval = $firsttime-$secondtime;
$dif=round(abs($interval) / 60);
if ($dif < 60 || $dif < -60 )
{
if ($dif!==0)
{
return true;
}
else
{
return false;
}
}
}
This is the place, where i filter after paid = 0 and does not contain anything in the error field.
foreach ($arrayoftransationcs as $key)
{
if($key['paid'] == 0 && empty($key['error']))
{
$ids[] = $key['transactionid'];
}
}
But I am not sure it is the right approach, I did it with the code. For technically, I select all scan which has the same membership number, restaurant with "double" in the field error, if it is paid = 1 or 0, does not matter .. this is not right, i think.
So I'm missing something here, checking if there is paid = 0 scan here if yes, check if there is a paid = 1 scan within the next 60min having the same medlem_id and restaurant. If so, then mark them as doubles.