i am new in Php 7 , i am trying to write a function that checks in the database if a product has a qte in a certain month(extracted from date field) but in the case that in that the product date do not have this month so all data have to be added to the array but the qte must be set to zero to the value of that month , i got results only of the existing qte in the months , but the results of the months that the products do not have any qte are not in my final array
Here what i have tried:
public function getProductsStatsByMonths() {
$today = date("Y-m-d");
$date_arr = explode("-", $today);
$year = $date_arr[0];
$month = $date_arr[1];
$day = $date_arr[2];
$prods = array();
$months = array(
1 ,
2 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 ,
10 ,
11 ,
12
);
foreach ($months as $month){
$stmt = $this->conn->prepare("SELECT sum(qte) as total,ligne, produit,date,heure,qte FROM production where YEAR(date) = ? and MONTH(date) = ? group by LOWER(ligne) ");
$stmt->execute([$year,$month]);
while( $row = $stmt->fetch(pdo::FETCH_ASSOC)){
array_push($prods, new ProductionByMonth(0,$row["ligne"],$row["produit"],$row["date"],$row["heure"],$row["qte"],$month,$year));
}
}
echo json_encode($prods);
}
the result im obtaining:
[
{
"id": 0,
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-08-10",
"heure": "10:00",
"qte": "130",
"month": 8,
"year": "2021"
},
{
"id": 0,
"ligne": "Eau",
"produit": "Safia 1.5",
"date": "2021-08-10",
"heure": "14:00",
"qte": "200",
"month": 8,
"year": "2021"
},
{
"id": 0,
"ligne": "Lait",
"produit": "Vitalait 1/2",
"date": "2021-08-10",
"heure": "8:00",
"qte": "80",
"month": 8,
"year": "2021"
},
{
"id": 0,
"ligne": "Salami",
"produit": "Mazraa",
"date": "2021-08-10",
"heure": "8:00",
"qte": "100",
"month": 8,
"year": "2021"
},
{
"id": 0,
"ligne": "Yaourt",
"produit": "Delice",
"date": "2021-08-10",
"heure": "12:00",
"qte": "150",
"month": 8,
"year": "2021"
}
]
for example this row contains this product that have a qte in the month 8:
{
"id": 0,
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-08-10",
"heure": "10:00",
"qte": "130",
"month": 8,
"year": "2021"
},
my goal is to add extra rows for this same product for the rest of the months but set qte for them as "0"
my goal is to make sure that my array in the end contains also the months that there is no qte (quantity) , and the value of qte must be set to zero and the data must be displayed like all data that exists except the value of the field qte