正确循环查询以在电子表格/ excel

时间:2018-02-06 16:23:55

标签: php mysql excel phpspreadsheet

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

这是一张看起来像的图片: enter image description here

我无法将其转换为创建我需要的公式。基本上,我需要在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');

1 个答案:

答案 0 :(得分:1)

我希望包含数据和分析的用户数组是最好的方法。

$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;
}

然后迭代用户条目以计算平均值,中位数和模式(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);
    }
}

然后,只需要决定如何在工作表上输出数据。