php - Query missing months in which we haven't sold anything? -
my query is:
$sql="select count(`variant`) tsold, monthname(`sold_date`) mname `vehicle_sold` group month(`sold_date`) "; $result = mysqli_query($conn,$sql); while($row = mysqli_fetch_array($result)) { echo $row["mname"],"---", $row["tsold"],"<br />";
}
which gives me below result:
january---1 february---2 march---7 april---11 may---6 july---1
there no sales in june want query return "june---0" example. if shows next months december it's ok. i'd following output:
january---1 february---2 march---7 april---11 may---6 june---0 july---1 aug---0 sept---0 oct---0 nov---0 dec---0
i assume have no data in database if did not sell anything. hence bit hard generate month without info.
you want array months, corresponding amount of sales. suggest make this:
prepare array months values on 0 default (you can make array list 'nicer', example).
$data['march'] = 0; $data['april'] = 0;
now can iterate did
while($row = mysqli_fetch_array($result)) { $data[$row["mname"]] = $row["tsold"]; }
if not filled database, value still 0. otherwise gets overwritten database value.
Comments
Post a Comment