Ok, so I am having trouble coming up with a solution to my problem. Basically, what I am trying to accomplish is to total the number of registrations in the last twelve months, starting with the current month.
First, since I cannot guarantee that each month would have a return, I built an array and populated it with my query results. I was able to pull all the data that I needed, but I am having trouble "sorting" them as you will.
I want to be able to display it in this manner, ex:
It is currently october so the months would list like this:
Nov 2014, Dec 2014, Jan 2015, Feb 2015, March 2015, ... October 2015.
Using this array
$months = array(
'1'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'2'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'3'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'4'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'5'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'6'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'7'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'8'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'9'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'10'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'11'=>array('TOTAL'=>0, 'YEAR'=>NULL),
'12'=>array('TOTAL'=>0, 'YEAR'=>NULL)
);
And populating it with the results I get:
key: 1 (January) total: 17 year: 2015
key: 2 (February) total: 20 year: 2015
key: 3 (March) total: 23 year: 2015
key: 4 (April) total: 29 year: 2015
key: 5 (May) total: 26 year: 2015
key: 6 (June) total: 26 year: 2015
key: 7 (July) total: 26 year: 2015
key: 8 (August) total: 24 year: 2015
key: 9 (September) total: 22 year: 2015
key: 10 (October) total: 24 year: 2015
key: 11 (November) total: 30 year: 2014
key: 12 (December) total: 42 year: 2014
Here's the query/loop
$query = "SELECT MONTH(DATE_ADDED) as MONTH_NUMBER, MONTHNAME(DATE_ADDED) as MONTH_NAME, COUNT(*) as TOTAL_REGISTRATIONS, YEAR(DATE_ADDED) AS YEAR FROM MEMBERS WHERE DATE_ADDED >= (CURDATE() - INTERVAL (DAY(CURDATE()) - 1) DAY) - INTERVAL 11 MONTH GROUP BY MONTH(DATE_ADDED) ORDER BY DATE_ADDED ASC";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$months[$row['MONTH_NUMBER']]['NAME'] = $row['MONTH_NAME'];
$months[$row['MONTH_NUMBER']]['TOTAL'] = $row['TOTAL_REGISTRATIONS'];
$months[$row['MONTH_NUMBER']]['YEAR'] = $row['YEAR'];
$months[$row['MONTH_NUMBER']]['MONTH_NUM'] = $row['MONTH_NUMBER'];
}
foreach($months as $key=>$data){
echo 'key: '.$key. ' ('.$data['NAME'].') total: '.$data['TOTAL'].' year: '.$data['YEAR'].'<br/>';
}
exit;
Edit: So far, I've been able to accomplish this:
key: 12 (December) total: 42 year: 2014
key: 11 (November) total: 30 year: 2014
key: 10 (October) total: 24 year: 2015
key: 9 (September) total: 22 year: 2015
key: 8 (August) total: 24 year: 2015
key: 7 (July) total: 26 year: 2015
key: 6 (June) total: 26 year: 2015
key: 5 (May) total: 26 year: 2015
key: 4 (April) total: 29 year: 2015
key: 3 (March) total: 23 year: 2015
key: 2 (February) total: 20 year: 2015
key: 1 (January) total: 17 year: 2015
using this code:
function sortArray(array $a, array $b) {
if($a['YEAR'] <= $b['YEAR'] && $a['MONTH_NUM'] < $b['MONTH_NUM']){
return 1;
}elseif($a['YEAR'] <= $b['YEAR'] && $a['MONTH_NUM'] > $b['MONTH_NUM']){
return -1;
}elseif($a['YEAR'] >= $b['YEAR'] && $a['MONTH_NUM'] > $b['MONTH_NUM']){
return -1;
}elseif($a['YEAR'] >= $b['YEAR'] && $a['MONTH_NUM'] < $b['MONTH_NUM']){
return 1;
} else {
return 0;
}
}
// Sort
uasort($months, 'sortArray');
Edit: You can see it sorts by the year properly, but lists the months the wrong way.
Edit: Desired Output
key: 11 (November) total: 00 year: 2014
key: 12 (December) total: 00 year: 2014
key: 1 (January) total: 00 year: 2015
key: 2 (February) total: 00 year: 2015
key: 3 (March) total: 00 year: 2015
key: 4 (April) total: 00 year: 2015
key: 5 (May) total: 00 year: 2015
key: 6 (June) total: 00 year: 2015
key: 7 (July) total: 00 year: 2015
key: 8 (August) total: 00 year: 2015
key: 9 (September) total: 00 year: 2015
key: 10 (October) total: 00 year: 2015
Ultimately, I will be displaying these in a bar graph showing the total per month over the last year.
I don't know if this is the best way to go about doing this, any suggestions are welcome.