【发布时间】:2018-01-23 10:58:54
【问题描述】:
我现在正在为我的图表创建一个 json 文件。我想要一个类似
的输出[{"ts":"九月","ph":23},{"ts":"七月","ph":13}]
其中 9 月的 ph 值相当于值:10、8、2、3,而 7 月的 ph 值相当于我数据库中的 10 和 3。
但现实是我只得到了这个输出
[{"ts":"九月","ph":23}]
来自下面的代码。我想添加 7 月的 ph 值。
$sumsep = 0;
$sumjul = 0;
while($row = mysqli_fetch_array($result))
{
/* Push the results in our array */
// $point = array("ts" => date('m',strtotime($row['time_stamp'])) ,"ph" => $row['ph']);
$monthNum = date('m',strtotime($row['time_stamp']));
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
if(($monthName=="September")){
$data_points = array();
$sumsep += $row['ph'];
$point = array("ts" => $monthName,"ph" => $sumsep);
array_push($data_points,$point);
}
}
请!!我需要你的帮助!!!
【问题讨论】:
-
不要在循环中定义你的
$data_points数组,你会在每次迭代时覆盖它。为什么你的循环中有一个条件?数学应该由数据库完成:SUM() ... GROUP BY ... -
计算每个月的ph总和,应该在数据库端做一些查询,是的,在while循环之外定义你的$data_points
-
感谢您的关注,我非常感谢..因为我在数组中工作,所以数据库上的 SUM() 不起作用,它必须是 $sumsep += $row['ph ']; .因为我需要总结数组