i am new to PHP OOP , i have a database that contains products, the goal is to extract for every product grouped by ligne field all the months data of the products, after i executed the result i have been surprised that only the data of the month 8 is displayed while there are some products that have data in months like 4 or 6.
Here the database rows:
Here the json Result array and we can observe that some products qte in some months is displayed as zero while their qte is not zero in some cases
[
{
"id": "8",
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-08-11",
"heure": "10:00",
"qte": "380",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 380
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
},
{
"id": "6",
"ligne": "Eau",
"produit": "Safia 1.5",
"date": "2021-08-11",
"heure": "14:00",
"qte": "320",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 320
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
},
{
"id": "12",
"ligne": "Lait",
"produit": "Vitalait 1/2",
"date": "2021-08-11",
"heure": "8:00",
"qte": "379",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 379
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
},
{
"id": "10",
"ligne": "Salami",
"produit": "Mazraa",
"date": "2021-08-11",
"heure": "8:00",
"qte": "570",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 570
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
},
{
"id": "4",
"ligne": "Yaourt",
"produit": "Delice",
"date": "2021-08-11",
"heure": "12:00",
"qte": "1020",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 1020
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
}
]
Here the Php Code :
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
);
$stmt = $this->conn->prepare("SELECT SUM(qte) as total , id ,ligne, produit, date, heure FROM production where YEAR(date) = ? GROUP BY LOWER(ligne) ");
$stmt->execute([$year]);
while( $row = $stmt->fetch(pdo::FETCH_ASSOC)){
$date_arrrow = explode("-", $row["date"]);
$rowmonth = $date_arrrow[1];
$ligne = $row["ligne"];
$produit = $row["produit"];
$p = new ProductionByMonth($row["id"],$row["ligne"],$row["produit"],$row["date"],$row["heure"],$row["total"],-1,$year);
$prodmonths = array();
foreach ($months as $month){
if(($rowmonth == $month) ){
array_push($prodmonths,new Months($month,(int) $row["total"]));
// array_push($prods, new ProductionByMonth($row["id"],$row["ligne"],$row["produit"],$row["date"],$row["heure"],$row["total"],$month,$year));
}else if ($rowmonth != $month){
array_push($prodmonths,new Months($month,0));
// array_push($prods, new ProductionByMonth($row["id"],$row["ligne"],$row["produit"],$row["date"],$row["heure"],0,$month,$year));
}
}
$p->months = $prodmonths;
array_push($prods,$p);
}
echo json_encode($prods);
}
My wish is to find a way to correctly extract all the correct qte in the json array for every product grouped by ligne field
