0

我目前正在尝试修复此脚本,以根据来自 mySql 查询的当前数据创建一个包含公式的电子表格。查询得到了应有的结果,我终于得到了循环来正确打印行。

这是看起来像的图像: 在此处输入图像描述

我无法将其转换为创建我需要的公式。基本上,我需要对 C、D、E 和 F 的 4 个主要列中的数字进行均值、中位数和众数。

我相信这需要通过关联数组中的索引来完成,但我并不积极。如图所示,B 列是用户 ID,所以我需要按此分组。基本上,为其他用户说If this is the last record for User ID 48, then create a cell for =average(), =Mean() and =median()并继续。在下一个用户开始之前,我想要一个用于 4 列(C、D、E 和 F)的均值、中位数和众数的单元格。

我以前有这个数组和while循环,但我无法让它迭代,它继续打印相同的记录:

          $conn = new mysqli($servername, $username, $password, $dbName);

            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }
            echo "Connected successfully";

            $sql = "
            select 
             concat(u.first_name, '  ', u.last_name) as Name,
             t.ext_id as ID,
             t.total_talk_time_minutes as TalkTime,
             t.total_outbound as Outbound,
             t.total_inbound as Inbound,
             t.dealers_contacted as Dealers,
             t.date_of_report as Date
            From
                ambition.ambition_totals t
            INNER JOIN
                ambition.ambition_users u
            ON 
                t.extension = u.extension
            where
                date_of_report 
            between
                curdate() - interval 5 day and curdate()"  or die(mysqli_error( $conn));

            $result=mysqli_query($conn,$sql);

            $spreadsheet = new Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();
            $sheet ->getColumnDimension('A') -> setAutoSize(true);
            $sheet ->getColumnDimension('B') -> setAutoSize(true);
            $sheet ->getColumnDimension('C') -> setAutoSize(true);
            $sheet ->getColumnDimension('D') -> setAutoSize(true);
            $sheet ->getColumnDimension('E') -> setAutoSize(true);
            $sheet ->getColumnDimension('F') -> setAutoSize(true);
            $sheet ->getColumnDimension('G') -> setAutoSize(true);

            $sheet->setCellValue('A1', 'Name');
            $sheet->setCellValue('B1', 'User ID');
            $sheet->setCellValue('C1', 'Talk Time Per Call');
            $sheet->setCellValue('D1', 'Outbound Calls');
            $sheet->setCellValue('E1', 'Inbound Calls');
            $sheet->setCellValue('F1', 'Dealer Contacts');
            $sheet->setCellValue('G1', 'Date');


            $data = [];

            while ($row = mysqli_fetch_assoc($result)) {
                if (!isset($data[$row['ID']]))
                    $data[$row['ID']] = ['records' => [], 'mmm' => []];
                $data[$row['ID']]['records'] = $row;
            }

            $analyze = ['TalkTime', 'Outbound', 'Inbound', 'Dealers'];

             foreach ($data AS $user) {
                 foreach ($analyze AS $attr) {
                     $set = []; 
                     foreach ($user['records'] AS $record){
                        $set[] = $record[$attr];
                        $user['mmm'][$attr] = [];
                        $user['mmm'][$attr]['mean'] = calcMean($set);
                        $user['mmm'][$attr]['median'] = calcMedian($set);
                        $user['mmm'][$attr]['mode'] = calcMode($set);
                    }
                }
            }

            var_dump($record);


            mysqli_close($conn);

            $writer = new xlsx($spreadsheet);
            $writer->save('Coaching Report - Weekly.xlsx');
4

1 回答 1

1

I'd expect an array of users containing the data and analysis is the best way to go.

$data = [];
while ($row = mysqli_fetch_assoc($result)) {
    // New user? Build data structure
    if (!isset($data[$row['ID']]))
        $data[$row['ID']] = ['records' => [], 'mmm' => []];
    // Collect rows
    $data[$row['ID']]['records'][] = $row;
}

And then iterate over user entries in order to calculate the mean, median, and mode (mmm):

$analyze = ['TalkTime', 'Outbound', 'Inbound', 'Dealers'];
foreach ($data AS &$user) {
    foreach ($analyze AS $attr) {
        $set = []; // Set of data for this attribute
        foreach ($user['records'] AS $record)
            $set[] = $record[$attr];
        $user['mmm'][$attr] = [];
        $user['mmm'][$attr]['mean'] = calcMean($set);
        $user['mmm'][$attr]['median'] = calcMedian($set);
        $user['mmm'][$attr]['mode'] = calcMode($set);
    }
}

Then it's just a matter of deciding how you want to output the data on your sheet.

于 2018-02-06T16:56:06.850 回答