The code I am using is below:
Code: Select all
<?php
include("connect.php");
$result = mysql_query("SELECT count(*) as ordercount, DATE_FORMAT(date_ordered, '%M %Y') as yearmonth FROM cases WHERE user_code!='001' AND user_code!='149' AND user_code!='150' AND YEAR(date_ordered)>=2006 GROUP BY LEFT(DATE(date_ordered), 7)");
echo "January 2006 = 0<br>";
echo "February 2006 = 0<br>";
$i = "3";
while($row = mysql_fetch_assoc($result)) {
echo $row["yearmonth"] . " = " . $row["ordercount"] . "<br>";
if($i == "12") {
$i = "0";
$year = explode(" ", $row["yearmonth"]);
$result1 = mysql_query("SELECT count(*) as ordercount FROM cases WHERE user_code!='001' AND user_code!='149' AND user_code!='150' AND YEAR(date_ordered)=" . $year["1"] . " GROUP BY LEFT(DATE(date_ordered), 4)");
$row1 = mysql_fetch_assoc($result1);
echo "<br><b>Total For " . $year["1"] . " = " . $row1["ordercount"] . "</b>";
echo "<hr>";
}
$i++;
}
?>January 2006 = 0
February 2006 = 0
March 2006 = 9
April 2006 = 12
May 2006 = 58
June 2006 = 166
July 2006 = 155
August 2006 = 243
September 2006 = 191
October 2006 = 208
November 2006 = 199
December 2006 = 358
Total For 2006 = 1599
--------------------------------------------------------------------------------
January 2007 = 367
February 2007 = 364
March 2007 = 423
April 2007 = 474
May 2007 = 438
June 2007 = 378
July 2007 = 380
August 2007 = 21
I would like to make this print out totals for each year. I have to manually print the first two months of 2006 because there was nothing ordered in those months, however I still want it to show that none were ordered.
I would like the final output to look like this:
January 2006 = 0
February 2006 = 0
March 2006 = 9
April 2006 = 12
May 2006 = 58
June 2006 = 166
July 2006 = 155
August 2006 = 243
September 2006 = 191
October 2006 = 208
November 2006 = 199
December 2006 = 358
Total For 2006 = 1599
--------------------------------------------------------------------------------
January 2007 = 367
February 2007 = 364
March 2007 = 423
April 2007 = 474
May 2007 = 438
June 2007 = 378
July 2007 = 380
August 2007 = 21
Total For 2007 = 2845
--------------------------------------------------------------------------------