2017-10-17 109 views
-1

我有一個腳本,當前運行SQL查詢並將選定的數據寫入CSV。這是完美的。但是,我需要對其進行修改,以便寫入數據(按客戶服務代表擴展名分組),並在每個代理的記錄下添加一個新行並彙總其數量。使用數組將總計添加到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)); 
} 

回答

0

你爲fetch_array的參數中使用常量MYSQLI_NUM,但你要MYSQLI_ASSOC,這樣一個關聯數組返回,而不是一個整數索引數組。

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

我這樣做,並繞過了錯誤,但現在它給一個數組字符串轉換通知?我的CSV只是列'數組' –

+0

我想你的意思'array_values($ userDetail)'而不是$ userDetails。這仍然只是輸出您想要在csv中的計數。它看起來像在這個腳本中的某處,你需要將每個$ row數組寫入到csv中。 – drussey