【发布时间】:2021-08-11 00:42:48
【问题描述】:
我是 Php 7 的新手,我正在尝试编写一个函数来检查数据库中产品是否在某个月份有 qte(从日期字段中提取),但如果产品日期没有有这个月,所以所有数据都必须添加到数组中,但 qte 必须设置为零到那个月的值,我只得到了月份中现有 qte 的结果,但是产品做的月份的结果没有任何 qte 不在我的最终数组中
这是我尝试过的:
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);
}
我得到的结果:
[
{
"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"
}
]
例如,此行包含此产品,该产品在第 8 个月有一个 qte:
{
"id": 0,
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-08-10",
"heure": "10:00",
"qte": "130",
"month": 8,
"year": "2021"
},
我的目标是在剩下的几个月里为同一产品添加额外的行,但将它们的 qte 设置为“0”
我的目标是确保我的数组最后还包含没有 qte (quantity) 的月份,并且 qte 的值必须设置为零,并且数据必须像所有存在的数据一样显示,除了字段qte的值
【问题讨论】:
标签: php