使用PHP和MySQL查询结果计算运行总计

时间:2019-10-01 13:10:20

标签: php arrays mysqli running-total

我正在尝试创建一个滚动使用情况表,在其中使用MySQL查询迭代24个月的数据,然后将结果放入要导出到报表中的数组中,但是我无法正确使用该情况获得总计。我在下面添加了我的代码,并删除了数据库信息,但是该报告目前仅在正常使用的情况下工作,只是在滚动方面苦苦挣扎。

//rolling usage table
//loop through 24 months of data 
for($i=0;$i<24;$i++){
    //Create indexes and dates for searching and storing.
    $epochTime = strtotime($myDate . "-$i months");
    $lastDay = date("t", $epochTime);
    $currentRollingMonthIndex = date("F Y", $epochTime);
    $endDate = date("Y-m-", $epochTime) . $lastDay;
    $beginDate = date("Y-m-01", $epochTime);

    //Collect the current month index.
    $rollingDates[$i] = $currentRollingMonthIndex;
    $rollingUsage = 0;

    //list all of the active products currently and their info
    $sql = "MY QUERY IS HERE";
    $res = $db->query($sql) or die($db->error()."<pre>$sql</pre>");

    //get the information from the db for each product
    while($row = $db->fetch_assoc($res)) {

    $id = isset($row['id']) ? $row['id'] : '';
      $usage = isset($row['usage']) ? $row['usage'] : '';

    $rollingUsageArr[$id][$currentRollingMonthIndex]['usage'] = $usage;
    }
}

0 个答案:

没有答案