I'm sure there is a more elegant way of doing this, but it was something that my feeble PHP skills could barely handle. I am creating a report based on month. And the db did not have a values for every month. Thus to generate a report, I needed to insert months with 0 values.
I'm curious if there is a better way of handling missing date elements in an array.
$r='2009-01-01';
$s='2009-12-31';
echo "<H2>Report by Month</H2>";
$monthturn = 'SELECT monthname(`Date`) as Month, Count(*) as Number FROM `main` WHERE `Date` Between "'.$r.'" and "'.$s.'" Group by Month (`Date`)';
$monthresult = mysql_query($monthturn) or die(mysql_error());
$months = array("January","February","March","April","May","June","July","August","September","October","November","December");
$nummonths = array("01","02","03","04","05","06","07","08","09","10","11","12");
While ($row = mysql_fetch_assoc($monthresult)){
$value[] = $row['Number'];
$labels[] = date("m",strtotime($row['Month'])); //convert month name to a two digit number like 08
}
$value = array_pad($value,12,0); //Ensure array has 12 elements, with 0 for any array <12
$combo = array("month1"=>$labels, "value1"=>$value); //merge the arrays
$delt = array_diff($nummonths,$combo['month1']); //find missing months.
$combo[month1]=array_merge($combo['month1'],$delt); //combine existing months and missing months
array_multisort($combo['month1'],$combo['value1']); //sort array with their values
$monthsname = array("monthname"=>$months); //create new array with names of months
$combo = array_merge($combo,$monthsname); //combine existing with new array
print_r($combo)