PHP,sql查询到CSV,设置总计行的格式

PHP,sql查询到CSV,设置总计行的格式,php,mysql,csv,Php,Mysql,Csv,我有一个工作脚本,它在执行时运行以下SQL查询,然后将其写入CSV。所有这些都非常有效 但是,我想稍微调整一下格式。它目前按员工记录结果,按昨晚的订单记录。我希望在所有代理记录的末尾有一行,上面写着“代理名称:总计”,然后汇总他们记录的总计 例如,每一行都有自己的名称、电话分机,然后是几个指标,这些指标要么为空,要么为“x”,最后是一个表示电话时间的数字。所以我想在适当的字段中加上x的总数,加上电话的持续时间,最后加上通话总数(这将是该员工记录的计数) 我不知道这是否应该在SQL、CSV代码块中

我有一个工作脚本,它在执行时运行以下SQL查询,然后将其写入CSV。所有这些都非常有效

但是,我想稍微调整一下格式。它目前按员工记录结果,按昨晚的订单记录。我希望在所有代理记录的末尾有一行,上面写着“代理名称:总计”,然后汇总他们记录的总计

例如,每一行都有自己的名称、电话分机,然后是几个指标,这些指标要么为空,要么为“x”,最后是一个表示电话时间的数字。所以我想在适当的字段中加上x的总数,加上电话的持续时间,最后加上通话总数(这将是该员工记录的计数)

我不知道这是否应该在SQL、CSV代码块中完成,或者是否有更好的方法使用PHP存储度量并以编程方式完成

我是这里的新手,通常只依赖MySQL workbench中的查询,所以非常感谢您的帮助

以下是脚本:

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

您将循环到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));
}

您将循环到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));
}

同时循环每个用户写入CSV文件。制作一个数组,存储用户的详细信息,并在CSV文件末尾回显,同时循环每个用户以写入CSV文件。制作一个数组并存储用户的详细信息,并在CSV文件的末尾回显。我将很快尝试此操作,并让您知道。谢谢我没办法让它工作。我已经将它添加到查询和现有CSV块之间的代码中,但它发现$row['extension']是未定义的variable@TomN. 请检查分机的列名。我假设了扩展名,但您可能得到了另一个名称,或者您可能更改了列名是的,这是正确的列。但是,我在这个表上为扩展名和报告的日期设置了一个唯一的索引,我不知道这是否与itI有关。我用我现在使用的代码更新了我的问题。我将很快尝试,并让您知道。谢谢我没办法让它工作。我已经将它添加到查询和现有CSV块之间的代码中,但它发现$row['extension']是未定义的variable@TomN. 请检查分机的列名。我假设了扩展名,但您可能得到了另一个名称,或者您可能更改了列名是的,这是正确的列。然而,我在这个表上为扩展名和报告的日期设置了一个唯一的索引,我不知道这是否与itI有关。我用我现在使用的代码更新了我的问题