使用数组将总计添加到CSV

时间:2017-10-17 12:51:04

标签: php mysql csv

我有一个脚本,当前运行SQL查询并将选定的数据写入CSV。这很完美。但是,我需要修改它以便它写入数据(按客户服务代表扩展分组)并在每个代理的记录下添加一个新行并总计它们的数字。

我已经在Excel中格式化了我的一个CSV以显示我需要的内容,但我需要脚本自动执行此操作。我有2列只有数字可以求和,但我的其他列有一个指标' x'或者他们是空白的。因此,在excel中,我必须制定这个来计算每个具有' x'在里面。我不确定如何在我的脚本中通过PHP或SQL以编程方式完成所有操作。

以下是目前编写的CSV: enter image description here

这是在我按照我想要的格式之后(我只用粗体和颜色显示总计。我不需要在脚本中执行此操作): enter image description here

因此,我开始使用工作脚本的修改版本来尝试使用新的总行来编写CSV,但是当我在PowerShell中运行它时,它表示有一个未定义的索引用于'扩展' ,在我在$extension上声明变量$row['extension']的行上。有人可以帮我把这些碎片放在一起吗?

这是当前失败的脚本:

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


    $fp = fopen('newDailyTest.csv', 'w');
    $userDetails = array();
    while($row = $result->fetch_array(MYSQLI_NUM)){
        /*  take the extension as the key to store its respective counts */
        $extension = $row['extension']; //this is where the undefined index error is
        /*  while loop calculations  */
        if(!isset($userDetails[$extension])){

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

1 个答案:

答案 0 :(得分:0)

你使用常量MYSQLI_NUM作为fetch_array的参数,但是你需要MYSQLI_ASSOC以便返回一个关联数组而不是一个整数索引数组。

while($row = $result->fetch_array(MYSQLI_ASSOC)){ //replace in this line