0

私の髪を引き裂く...次の配列の数字を合計する方法、これを試しましたが、うまくいきませんでした。ループ内でも試しましたが、何も得られません。そんなに難しくないと思いますが、なぜか取れません。

$sql = "SELECT queue_name,type,COUNT(uniqueid) AS calls FROM CallLog WHERE start_time     BETWEEN '2013-10-14 00:00:00' AND '2013-10-14 23:59:59' GROUP BY queue_name, type";

$stmt=$dbh->prepare($sql);
$stmt->execute();

$calls = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach($calls as $call){
$results[ $call['queue_name'] ][ $call['type'] ] = $call['calls'];

}

$totalCalls = array_sum($call['calls']);

echo '<pre>';
print_r($results);
echo '</pre>';
echo $totalCalls;
?>
Array
    (
    [Escalations] => Array
    (
        [abandoned] => 2
        [completed] => 3
        [redirected] => 1
    )

[Premium] => Array
    (
        [abandoned] => 7
        [completed] => 29
        [redirected] => 6
    )

[Standard] => Array
    (
        [abandoned] => 14
        [completed] => 41
        [redirected] => 53
    )

[Wingate Queue] => Array
    (
        [abandoned] => 2
        [completed] => 3
    )

[WorldMark] => Array
    (
        [abandoned] => 32
        [completed] => 100
        [redirected] => 82
    )

    )
4

6 に答える 6

2
<?php
$results = array(
    'Escalations' => array('abandoned' => 2,'completed' => 3,'redirected' => 1),
    'Premium' => array('abandoned' => 7,'completed' => 29,'redirected' => 6),
    'Standard' => array('abandoned' => 14,'completed' => 41,'redirected' => 53),
    'Wingate Queue' => array('abandoned' => 2,'completed' => 3),
    'WorldMark' => array('abandoned' => 32,'completed' => 100,'redirected' => 82)
);

$total_calls = 0;
foreach($results as $k=>$v){
    $total_calls += array_sum($v);
}

echo $total_calls;
于 2013-10-16T18:23:41.560 に答える
1

$callforループの外で定義されていません:

$totalCalls = array_sum($call['calls']);
                        ^^^^^

ループで合計することができます:

$totalCalls = 0;
foreach($calls as $call){
    $results[ $call['queue_name'] ][ $call['type'] ] = $call['calls'];
    $totalCalls += $call['calls'];
}
于 2013-10-16T18:20:51.940 に答える
1

これを試してみてください

$totalCalls = 0;
foreach($calls as $call){
    $results[ $call['queue_name'] ][ $call['type'] ] = $call['calls'];
    $totalCalls += $call['calls'];
}
于 2013-10-16T18:21:53.383 に答える
1

コードにバグがあります:

    $totalCalls = array_sum($call['calls']); 
// $call['calls'] is just a single value not an array

最も簡単な方法は次のとおりです。

$totalCalls = 0;
foreach($calls as $call){
$results[ $call['queue_name'] ][ $call['type'] ] = $call['calls'];
$totalCalls += $call['calls'];
}
于 2013-10-16T18:22:09.960 に答える
1

You can do this in your SQL query if you do not need the rest of the data elsewhere.

Something along the lines of:

SELECT 
    SUM(uniqueid) AS total
FROM 
    CallLog 
WHERE
    start_time BETWEEN '2013-10-14 00:00:00' AND '2013-10-14 23:59:59'
GROUP BY 
    queue_name, type";

Keep in mind this will return a single number, and not all the data you previously had.

于 2013-10-16T18:27:24.540 に答える
0

すべての呼び出しをカウントする簡単な方法は次のとおりです。

$totalCalls = 0;
foreach($calls as $call){
    $totalCalls += array_sum($call);
}

これにより、すべてのアイテムがカウントされます。

于 2013-10-16T19:12:05.640 に答える