PHP,sql查询到CSV,格式化行总数

时间:2017-10-10 14:33:13

标签: php mysql csv

我有一个工作脚本,在执行时会运行以下SQL查询,然后将其写入CSV。所有这一切都很完美。

但是,我想稍微调整格式。它目前通过员工和订单在昨晚之前写出结果。我想在所有代理商的记录末尾有一行说“Agent's Name:Totals”之类的东西,然后总结他们记录的总数。

例如,每一行都有自己的姓名,电话分机,然后是几个空白或有“x”的指标,最后一个数字代表手机上的时间。所以我想在相应的字段中加上x,在手机上添加持续时间,最后是总呼叫数(这将是该员工记录的数量)。

我不知道这是否应该在SQL中,在CSV代码块中完成,或者是否有更好的方法来使用PHP存储指标并以编程方式执行此操作。

我是这里的新手,通常只是依赖于MySQL工作台中的查询,所以非常感谢任何帮助。

这是脚本:

$result = mysqli_query($conn2,
"SELECT
      firstn
    , lastn
    , extension
    , Recieved
    , RecievedKnown
    , Outbound
    , outboundKnown
    , Missed
    , MissedKnown
    , CallingNumber
    , CalledNumber
    , starttime
    , endtime
      , duration
    , HOLDTIMESECS
    , TERMINATIONREASONCODE

FROM (
      SELECT
              u.firstn
            , u.lastn
            , c.extension
            , CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 'x' ELSE '' END AS Recieved
            , CASE WHEN LEGTYPE1 = 2 AND answered = 1 AND CALLINGPARTYNO = k.phone_number THEN 'x' ELSE '' END AS RecievedKnown
            , CASE WHEN ANSWERED = 1 AND LEGTYPE1 = 1 THEN 'x' ELSE '' END  AS Outbound
            , CASE WHEN LEGTYPE1 = 1 AND FINALLYCALLEDPARTYNO = k.phone_number THEN 'x' ELSE '' END AS outboundKnown
            , CASE WHEN Answered = 0 THEN 'x' ELSE '' END AS Missed
            , CASE WHEN ANSWERED = 0 AND CALLINGPARTYNO = k.phone_number THEN 'x' ELSE '' END AS MissedKnown
            , a.CALLINGPARTYNO AS CallingNumber
            , a.FINALLYCALLEDPARTYNO AS CalledNumber
            , b.starttime AS starttime
            , b.endtime AS endtime
            , b.duration
            , a.holdtimesecs
            , a.terminationreasoncode
      FROM ambition.session a
      INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
      INNER JOIN ambition.mxuser c ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
      INNER JOIN jackson_id.users u ON c.extension = u.extension
      LEFT JOIN ambition.known_numbers k ON a.callingpartyno = k.phone_number
      WHERE date(b.ts) >= curdate()
      AND LEGTYPE1 <> 12 -- This keeps the report from having blank spaces due to the 12 legtype.
      AND c.extension IN (7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)

      ) x
    ORDER BY lastn") or die(mysqli_error( $conn2));



    $userDetails = array();
    while($row = $result->fetch_array(MYSQLI_NUM)){
        /* Let me take the extension as the key to store its respective counts */
        $extension = $row['extension'];
        /* Now in your while loop do all the calculations for the user */
        if(!isset($userDetails[$extension])){
            /* Since this is the first time for the extension your doin the calculations */
            $userDetails[$extension]['missedCallCounts'] = 1; /* First time count */
        }else{
            $userDetails[$extension]['missedCallCounts'] += 1; /* Sum up the count */
        }
    }

    foreach($userDetails as $userDetail){
/* In the following line dump the respective userdetails to csv which will show summary */
fputcsv($fp, array_values($userDetails));
}

当前CSV的截图导致excel(省略名称/数字,但您仍然看到了这个想法): enter image description here

因此,对于扩展7312,前两行将是一个新行,如:

7312's Totals | 0 | 0 | 0 | 0 | 2 | 2 | - | - | - | - | 76 | 0

1 个答案:

答案 0 :(得分:2)

您循环进入sql查询以将其输出到CSV文件,此时将记录保存在通用数组中并在循环结束后转储。

例如:

$userDetails = array();
while($row = $result->fetch_array(MYSQLI_NUM)){
    /* Let me take the extension as the key to store its respective counts */
    $extension = $row['extension'];
    /* Now in your while loop do all the calculations for the user */
    if(!isset($userDetails[$extension])){
        /* Since this is the first time for the extension your doin the calculations */
        $userDetails[$extension]['missedCallCounts'] = 1; /* First time count */
    }else{
        $userDetails[$extension]['missedCallCounts'] += 1; /* Sum up the count */
    }
}

现在循环进入$ userDetails并将其转储到CSV

foreach($userDetails as $userDetail){
    /* In the following line dump the respective userdetails to csv which will show summary */
    fputcsv($fp, array_values($userDetails));
}