У меня есть mysql таблица:
id | datetime | status | etc
----------------------------
1 | 2020-03-03 | P
2 | 2020-03-08 | N
3 | 2020-03-12 | S
4 | 2020-03-12 | N
5 | 2020-03-13 | N
6 | 2020-03-13 | N
7 | 2020-03-13 | S
8 | 2020-03-14 | S
9 | 2020-03-14 | S
10 | 2020-03-14 | S
11 | 2020-03-14 | S
12 | 2020-03-14 | S
13 | 2020-03-14 | S
14 | 2020-03-15 | S
15 | 2020-03-15 | S
16 | 2020-03-15 | S
17 | 2020-03-15 | N
Мне нужно показать рост дел по статусу, поэтому я сделал:
SELECT DATE(date_created) as date, status, COUNT(*) as total FROM mytable WHERE id>0 GROUP BY DATE(date_created), status ORDER BY date_created ASC
И я реализовал этот php код: каждый промежуток времени может быть в один и тот же день, я думаю, что я получаю сообщение об ошибке, посмотрите на мой код:
$Return = array('d'=> array(),
'P' => array(),
'N' => array(),
'S' => array());
$diff = 0; $oldDay = ''; $lastDiff = -1;//used at the end code
foreach($selectData as $key => $val){
$dayMon = date('d/m',strtotime($val['date']));
if($dayMon!=$oldDay){//changed the day
if($oldDay!=''){//not 1st time
//set previous date total or 0 to status not used
foreach($listStatus as $temp){
$Return[$temp][$key-1-$diff] = isset($Return[$temp][$key-2-$diff]) ? $Return[$temp][$key-2-$diff] : 0;
}
}
$Return['d'][] = $dayMon;
$lastDiff = $diff;
$diff = 0;
$listStatus = array('P','N','S');
} else{
$diff++;
}
$oldVal = isset($Return[$val['status']][$key-1-$diff]) ? $Return[$val['status']][$key-1-$diff] : 0;
$Return[$val['status']][$key-$diff] = $oldVal + $val['total'];
//remove status used that day
$listStatus = array_diff($listStatus, array($val['status']));
$oldDay = $dayMon;
$lastKey = $key;
}
//set previous date total or 0 to status not used
foreach($listStatus as $temp){
$Return[$temp][$lastKey-lastDiff] = isset($Return[$temp][$lastKey-1-lastDiff]) ? $Return[$temp][$lastKey-1-lastDiff] : 0;
}
Я получаю около 3 первых строк, но некоторые ошибки на следующих, согласно:
Day 03/03 = had P=1, N=0, S=0
Day 08/03 = had P=1, N=1, S=0
Day 12/03 = had P=1, N=2, S=1
Notice: Undefined offset: 3 in mypage.php
Notice: Undefined offset: 3 in mypage.php
Notice: Undefined offset: 3 in mypage.php
Day 13/03 = had P=, N=, S=
Day 14/03 = had P=, N=, S=
Notice: Undefined offset: 5 in mypage.php
Notice: Undefined offset: 5 in mypage.php
Notice: Undefined offset: 5 in mypage.php
Day 15/03 = had P=, N=, S=