0

我有一个 php 脚本,它创建一个带有 2 个工作表的 excel 文件。第一张表是完美的,因为它为属于个人用户的记录制定值,所以我使用了一个关联数组,使用他们的用户 ID 作为索引。

第二张表只获取 5 天的部门总数,所以不是说“为每个 ID 创建一个记录数组并在该记录上制定”我想说“将所有 5 行加载为数组中的一个记录组,以便我可以根据只有那些记录”。我希望这是有道理的。

我的脚本现在的方式是将我的公式行(平均值、中位数和众数)放在所有 5 条记录之间,但在所有 5 条记录之后我只需要一个公式行。基本上我现在循环太多了,我不确定在哪里重构它。

这是脚本:

$resultData2 = array();

while($row2 = mysqli_fetch_assoc($result2)) 

  {
    $resultData2[$row2['TalkTime']][] = $row2;
  }

//Set starting row number
$rowNo2 = 1;

// //Iterate over the results for each unique id
foreach($resultData2 as $idRecords2)
    {
     $userFirstRow2 = $rowNo2+1;

    //Iterate over the records for this ID
    foreach($idRecords2 as $record2)
        {
            //Increment row number
            $rowNo2++;
            //Add record row to spreadsheet
            $worksheet1->setCellValue("A{$rowNo2}", $record2['TalkTime']);
            $worksheet1->setCellValue("D{$rowNo2}", $record2['Outbound']);
            $worksheet1->setCellValue("G{$rowNo2}", $record2['Inbound']);
            $worksheet1->setCellValue("J{$rowNo2}", $record2['Dealers']);
            $worksheet1->setCellValue("M{$rowNo2}", $record2['Date']);
            $worksheet1->setCellValue("N{$rowNo2}", $record2['Day']);

        }

    //Increment row number
    $rowNo2++;

    $worksheet1->setCellValue("A{$rowNo2}", "Mean");
    $worksheet1->setCellValue("B{$rowNo2}", "Median");
    $worksheet1->setCellValue("C{$rowNo2}", "Mode");

    $worksheet1->setCellValue("D{$rowNo2}", "Mean");
    $worksheet1->setCellValue("E{$rowNo2}", "Median");
    $worksheet1->setCellValue("F{$rowNo2}", "Mode");

    $worksheet1->setCellValue("G{$rowNo2}", "Mean");
    $worksheet1->setCellValue("H{$rowNo2}", "Median");
    $worksheet1->setCellValue("I{$rowNo2}", "Mode");

    $worksheet1->setCellValue("J{$rowNo2}", "Mean");
    $worksheet1->setCellValue("K{$rowNo2}", "Median");
    $worksheet1->setCellValue("L{$rowNo2}", "Mode");

    $rowNo++;
}
4

0 回答 0