Have been looking at this for ages, but cannot overcome the problem. Have found running total questions, but none deal with 2 queries...
$query = "SELECT d.DateAdded, #running_sum:=#running_sum + d.count AS running FROM (SELECT DateAdded, COUNT(*) AS 'count' FROM details WHERE MemberStatus = 'Active'GROUP BY DATE_FORMAT(DateAdded,'%Y-%m-%d') ORDER BY DateAdded ) d JOIN (SELECT #running_sum := 0 AS dummy) dummy;";
$result = mysqli_query($mysqli,$query);
$array = array();
while ($row2 = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$RunningTotal = $row2['running'];
foreach($mysqli->query('SELECT DateAdded, COUNT(*) FROM details GROUP BY DATE_FORMAT(DateAdded,"%Y-%m-%d")') as $row) {
$year = date("Y-m-d",strtotime($row['DateAdded']));
array_push($array,array('Year'=>$year, 'Numb'=>$row['COUNT(*)'],'Total'=>$RunningTotal));
}
}
echo json_encode($array);
Above is the closest I've come, but cannot get the output correct..
Current output of above is.. (obviously I have WHILE and FOREACH completely wrong!!)
[{"Year":"2016-10-27","Numb":"1","Total":"1
"},{"Year":"2016-10-31","Numb":"1","Total":"1
"},{"Year":"2016-11-02","Numb":"1","Total":"1
"},{"Year":"2016-11-05","Numb":"1","Total":"1
"},{"Year":"2016-11-07","Numb":"1","Total":"1
"},{"Year":"2016-11-08","Numb":"1","Total":"1
"},{"Year":"2016-11-09","Numb":"1","Total":"1
"},{"Year":"2016-11-10","Numb":"2","Total":"1
"},{"Year":"2016-11-16","Numb":"2","Total":"1
"},{"Year":"2016-11-20","Numb":"1","Total":"1
"},{"Year":"2016-11-22","Numb":"1","Total":"1
"},{"Year":"2016-12-05","Numb":"1","Total":"1
"},{"Year":"2016-12-06","Numb":"1","Total":"1
"},{"Year":"2016-12-07","Numb":"1","Total":"1
"},{"Year":"2016-10-27","Numb":"1","Total":"2
"},{"Year":"2016-10-31","Numb":"1","Total":"2
"},{"Year":"2016-11-02","Numb":"1","Total":"2
"},{"Year":"2016-11-05","Numb":"1","Total":"2
"},{"Year":"2016-11-07","Numb":"1","Total":"2
"},{"Year":"2016-11-08","Numb":"1","Total":"2
"},{"Year":"2016-11-09","Numb":"1","Total":"2
"},{"Year":"2016-11-10","Numb":"2","Total":"2
"},{"Year":"2016-11-16","Numb":"2","Total":"2
"},{"Year":"2016-11-20","Numb":"1","Total":"2
"},{"Year":"2016-11-22","Numb":"1","Total":"2
"},{"Year":"2016-12-05","Numb":"1","Total":"2
"},{"Year":"2016-12-06","Numb":"1","Total":"2
"},{"Year":"2016-12-07","Numb":"1","Total":"2
"},{"Year":"2016-10-27","Numb":"1","Total":"3
"},{"Year":"2016-10-31","Numb":"1","Total":"3
"},{"Year":"2016-11-02","Numb":"1","Total":"3
"},{"Year":"2016-11-05","Numb":"1","Total":"3
"},{"Year":"2016-11-07","Numb":"1","Total":"3
"},{"Year":"2016-11-08","Numb":"1","Total":"3
"},{"Year":"2016-11-09","Numb":"1","Total":"3
"},{"Year":"2016-11-10","Numb":"2","Total":"3
"},{"Year":"2016-11-16","Numb":"2","Total":"3
"},{"Year":"2016-11-20","Numb":"1","Total":"3
"},{"Year":"2016-11-22","Numb":"1","Total":"3
"},{"Year":"2016-12-05","Numb":"1","Total":"3
"},{"Year":"2016-12-06","Numb":"1","Total":"3
"},{"Year":"2016-12-07","Numb":"1","Total":"3
"},{"Year":"2016-10-27","Numb":"1","Total":"4
"},{"Year":"2016-10-31","Numb":"1","Total":"4
"},{"Year":"2016-11-02","Numb":"1","Total":"4
"},{"Year":"2016-11-05","Numb":"1","Total":"4
"},{"Year":"2016-11-07","Numb":"1","Total":"4
"},{"Year":"2016-11-08","Numb":"1","Total":"4
"},{"Year":"2016-11-09","Numb":"1","Total":"4
"},{"Year":"2016-11-10","Numb":"2","Total":"4
"},{"Year":"2016-11-16","Numb":"2","Total":"4
"},{"Year":"2016-11-20","Numb":"1","Total":"4
"},{"Year":"2016-11-22","Numb":"1","Total":"4
"},{"Year":"2016-12-05","Numb":"1","Total":"4
"},{"Year":"2016-12-06","Numb":"1","Total":"4
"},{"Year":"2016-12-07","Numb":"1","Total":"4
"},{"Year":"2016-10-27","Numb":"1","Total":"5
"},{"Year":"2016-10-31","Numb":"1","Total":"5
"},{"Year":"2016-11-02","Numb":"1","Total":"5
"},{"Year":"2016-11-05","Numb":"1","Total":"5
"},{"Year":"2016-11-07","Numb":"1","Total":"5
"},{"Year":"2016-11-08","Numb":"1","Total":"5
"},{"Year":"2016-11-09","Numb":"1","Total":"5
"},{"Year":"2016-11-10","Numb":"2","Total":"5
"},{"Year":"2016-11-16","Numb":"2","Total":"5
"},{"Year":"2016-11-20","Numb":"1","Total":"5
"},{"Year":"2016-11-22","Numb":"1","Total":"5
"},{"Year":"2016-12-05","Numb":"1","Total":"5
"},{"Year":"2016-12-06","Numb":"1","Total":"5
"},{"Year":"2016-12-07","Numb":"1","Total":"5
"},{"Year":"2016-10-27","Numb":"1","Total":"6
"},{"Year":"2016-10-31","Numb":"1","Total":"6
"},{"Year":"2016-11-02","Numb":"1","Total":"6
"},{"Year":"2016-11-05","Numb":"1","Total":"6
"},{"Year":"2016-11-07","Numb":"1","Total":"6
"},{"Year":"2016-11-08","Numb":"1","Total":"6
"},{"Year":"2016-11-09","Numb":"1","Total":"6
"},{"Year":"2016-11-10","Numb":"2","Total":"6
"},{"Year":"2016-11-16","Numb":"2","Total":"6
"},{"Year":"2016-11-20","Numb":"1","Total":"6
"},{"Year":"2016-11-22","Numb":"1","Total":"6
"},{"Year":"2016-12-05","Numb":"1","Total":"6
"},{"Year":"2016-12-06","Numb":"1","Total":"6
"},{"Year":"2016-12-07","Numb":"1","Total":"6
"},{"Year":"2016-10-27","Numb":"1","Total":"7
"},{"Year":"2016-10-31","Numb":"1","Total":"7
"},{"Year":"2016-11-02","Numb":"1","Total":"7
"},{"Year":"2016-11-05","Numb":"1","Total":"7
"},{"Year":"2016-11-07","Numb":"1","Total":"7
"},{"Year":"2016-11-08","Numb":"1","Total":"7
"},{"Year":"2016-11-09","Numb":"1","Total":"7
"},{"Year":"2016-11-10","Numb":"2","Total":"7
"},{"Year":"2016-11-16","Numb":"2","Total":"7
"},{"Year":"2016-11-20","Numb":"1","Total":"7
"},{"Year":"2016-11-22","Numb":"1","Total":"7
"},{"Year":"2016-12-05","Numb":"1","Total":"7
"},{"Year":"2016-12-06","Numb":"1","Total":"7
"},{"Year":"2016-12-07","Numb":"1","Total":"7
"},{"Year":"2016-10-27","Numb":"1","Total":"9
"},{"Year":"2016-10-31","Numb":"1","Total":"9
"},{"Year":"2016-11-02","Numb":"1","Total":"9
"},{"Year":"2016-11-05","Numb":"1","Total":"9
"},{"Year":"2016-11-07","Numb":"1","Total":"9
"},{"Year":"2016-11-08","Numb":"1","Total":"9
"},{"Year":"2016-11-09","Numb":"1","Total":"9
"},{"Year":"2016-11-10","Numb":"2","Total":"9
"},{"Year":"2016-11-16","Numb":"2","Total":"9
"},{"Year":"2016-11-20","Numb":"1","Total":"9
"},{"Year":"2016-11-22","Numb":"1","Total":"9
"},{"Year":"2016-12-05","Numb":"1","Total":"9
"},{"Year":"2016-12-06","Numb":"1","Total":"9
"},{"Year":"2016-12-07","Numb":"1","Total":"9
"},{"Year":"2016-10-27","Numb":"1","Total":"11
"},{"Year":"2016-10-31","Numb":"1","Total":"11
"},{"Year":"2016-11-02","Numb":"1","Total":"11
"},{"Year":"2016-11-05","Numb":"1","Total":"11
"},{"Year":"2016-11-07","Numb":"1","Total":"11
"},{"Year":"2016-11-08","Numb":"1","Total":"11
"},{"Year":"2016-11-09","Numb":"1","Total":"11
"},{"Year":"2016-11-10","Numb":"2","Total":"11
"},{"Year":"2016-11-16","Numb":"2","Total":"11
"},{"Year":"2016-11-20","Numb":"1","Total":"11
"},{"Year":"2016-11-22","Numb":"1","Total":"11
"},{"Year":"2016-12-05","Numb":"1","Total":"11
"},{"Year":"2016-12-06","Numb":"1","Total":"11
"},{"Year":"2016-12-07","Numb":"1","Total":"11
"},{"Year":"2016-10-27","Numb":"1","Total":"12
"},{"Year":"2016-10-31","Numb":"1","Total":"12
"},{"Year":"2016-11-02","Numb":"1","Total":"12
"},{"Year":"2016-11-05","Numb":"1","Total":"12
"},{"Year":"2016-11-07","Numb":"1","Total":"12
"},{"Year":"2016-11-08","Numb":"1","Total":"12
"},{"Year":"2016-11-09","Numb":"1","Total":"12
"},{"Year":"2016-11-10","Numb":"2","Total":"12
"},{"Year":"2016-11-16","Numb":"2","Total":"12
"},{"Year":"2016-11-20","Numb":"1","Total":"12
"},{"Year":"2016-11-22","Numb":"1","Total":"12
"},{"Year":"2016-12-05","Numb":"1","Total":"12
"},{"Year":"2016-12-06","Numb":"1","Total":"12
"},{"Year":"2016-12-07","Numb":"1","Total":"12
"},{"Year":"2016-10-27","Numb":"1","Total":"13
"},{"Year":"2016-10-31","Numb":"1","Total":"13
"},{"Year":"2016-11-02","Numb":"1","Total":"13
"},{"Year":"2016-11-05","Numb":"1","Total":"13
"},{"Year":"2016-11-07","Numb":"1","Total":"13
"},{"Year":"2016-11-08","Numb":"1","Total":"13
"},{"Year":"2016-11-09","Numb":"1","Total":"13
"},{"Year":"2016-11-10","Numb":"2","Total":"13
"},{"Year":"2016-11-16","Numb":"2","Total":"13
"},{"Year":"2016-11-20","Numb":"1","Total":"13
"},{"Year":"2016-11-22","Numb":"1","Total":"13
"},{"Year":"2016-12-05","Numb":"1","Total":"13
"},{"Year":"2016-12-06","Numb":"1","Total":"13
"},{"Year":"2016-12-07","Numb":"1","Total":"13
"},{"Year":"2016-10-27","Numb":"1","Total":"14
"},{"Year":"2016-10-31","Numb":"1","Total":"14
"},{"Year":"2016-11-02","Numb":"1","Total":"14
"},{"Year":"2016-11-05","Numb":"1","Total":"14
"},{"Year":"2016-11-07","Numb":"1","Total":"14
"},{"Year":"2016-11-08","Numb":"1","Total":"14
"},{"Year":"2016-11-09","Numb":"1","Total":"14
"},{"Year":"2016-11-10","Numb":"2","Total":"14
"},{"Year":"2016-11-16","Numb":"2","Total":"14
"},{"Year":"2016-11-20","Numb":"1","Total":"14
"},{"Year":"2016-11-22","Numb":"1","Total":"14
"},{"Year":"2016-12-05","Numb":"1","Total":"14
"},{"Year":"2016-12-06","Numb":"1","Total":"14
"},{"Year":"2016-12-07","Numb":"1","Total":"14
"},{"Year":"2016-10-27","Numb":"1","Total":"15
"},{"Year":"2016-10-31","Numb":"1","Total":"15
"},{"Year":"2016-11-02","Numb":"1","Total":"15
"},{"Year":"2016-11-05","Numb":"1","Total":"15
"},{"Year":"2016-11-07","Numb":"1","Total":"15
"},{"Year":"2016-11-08","Numb":"1","Total":"15
"},{"Year":"2016-11-09","Numb":"1","Total":"15
"},{"Year":"2016-11-10","Numb":"2","Total":"15
"},{"Year":"2016-11-16","Numb":"2","Total":"15
"},{"Year":"2016-11-20","Numb":"1","Total":"15
"},{"Year":"2016-11-22","Numb":"1","Total":"15
"},{"Year":"2016-12-05","Numb":"1","Total":"15
"},{"Year":"2016-12-06","Numb":"1","Total":"15
"},{"Year":"2016-12-07","Numb":"1","Total":"15
"},{"Year":"2016-10-27","Numb":"1","Total":"16
"},{"Year":"2016-10-31","Numb":"1","Total":"16
"},{"Year":"2016-11-02","Numb":"1","Total":"16
"},{"Year":"2016-11-05","Numb":"1","Total":"16
"},{"Year":"2016-11-07","Numb":"1","Total":"16
"},{"Year":"2016-11-08","Numb":"1","Total":"16
"},{"Year":"2016-11-09","Numb":"1","Total":"16
"},{"Year":"2016-11-10","Numb":"2","Total":"16
"},{"Year":"2016-11-16","Numb":"2","Total":"16
"},{"Year":"2016-11-20","Numb":"1","Total":"16
"},{"Year":"2016-11-22","Numb":"1","Total":"16
"},{"Year":"2016-12-05","Numb":"1","Total":"16
"},{"Year":"2016-12-06","Numb":"1","Total":"16
"},{"Year":"2016-12-07","Numb":"1","Total":"16
"}]
Year: Date Member Added
Numb: How many signed up on that particular date
Total: Running total of new signups
Below is what it should look like.. (just tided up formatting)
[{"Year":"2016-10-27","Numb":"1","Total":"1"},
{"Year":"2016-10-27","Numb":"1","Total":"2"},
{"Year":"2016-11-02","Numb":"1","Total":"3"},
{"Year":"2016-11-05","Numb":"1","Total":"4"},
{"Year":"2016-11-07","Numb":"1","Total":"5"},
{"Year":"2016-11-08","Numb":"1","Total":"6"},
{"Year":"2016-11-09","Numb":"1","Total":"7"},
{"Year":"2016-11-10","Numb":"2","Total":"9"},
{"Year":"2016-11-16","Numb":"2","Total":"11"},
{"Year":"2016-11-20","Numb":"1","Total":"12"},
{"Year":"2016-11-22","Numb":"1","Total":"13"},
{"Year":"2016-12-05","Numb":"1","Total":"14"},
{"Year":"2016-12-06","Numb":"1","Total":"15"},
{"Year":"2016-12-07","Numb":"1","Total":"16"}]
Any assistance would be greatly appreciated. This query is beyond my me.
Helps if you Google the right Question... Just combined two into one....
;-(
$myArray = array();
if ($result = $mysqli->query("SELECT DATE_FORMAT(DateAdded, '%Y-%m-%d') AS Year, COUNT(*) AS Numb, #running_sum:=#running_sum + d.count AS Total FROM (SELECT DateAdded, COUNT(*) AS 'count' FROM details WHERE MemberStatus = 'Active' AND PaymentStatus NOT REGEXP 'Not' GROUP BY DateAdded ORDER BY DateAdded) d JOIN (SELECT #running_sum := 0 AS dummy) dummy GROUP BY DATE_FORMAT(DateAdded,'%Y-%m-%d');")) {
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}