Search code examples
phpsqlarrayssum

Sum a single column of values in a 2d array


I am trying to sum all the SELECT COUNT from DB in order to get the total hour worked by an employee. $theWeek contains the days of current week in timestamp format, and $vendeur is the employee id got from HTML and JS.

This is what i get when i print_r($weekStat), which is fine because on Wednesday (key n°2) the employee worked 6 hours and the rest 0:

Array
(
    [0] => Array
        (
            [sum_count] => 0
        )

    [1] => Array
        (
            [sum_count] => 0
        )

    [2] => Array
        (
            [sum_count] => 6
        )

    [3] => Array
        (
            [sum_count] => 0
        )

    [4] => Array
        (
            [sum_count] => 0
        )

    [5] => Array
        (
            [sum_count] => 0
        )

    [6] => Array
        (
            [sum_count] => 0
        )

)

The problem is when I echo($total); & echo((int)$value); I get 0 for each $total and only 1 for each $value. i need to get $total = 6

Here is my code :

     function getHisStats($vendeur, $theWeek){
        $connexion = connexion();
        $weekStat = array(); // stock les plannings de chaque semaine
 
        $somme = 0;
        $total = 0;

        // On récupere chaque jour de la semaine et on assigne un planning à chaque jour
        for($i = 0; $i < 7; $i++){
            // Creating YY-MM-DD format for fetch in DB
            $dateCurrent = getdate($theWeek[$i]);
            $day = $dateCurrent['mday'];
            $month = $dateCurrent['mon'];
            $year = $dateCurrent['year'];

            // Used to fetch date
            $jour = $year. '-' . $month . '-' . $day;

            // Counting the number of instances of id_planning
            $request_travail = $connexion->prepare("SELECT COUNT(id_planning) AS sum_count FROM plannings WHERE id_employe = ? AND date_planning = ? AND (id_affectation = ? OR id_affectation = ? OR id_affectation = ? OR id_affectation = ?) AND id_validation = ?");
            $request_travail->execute(array($vendeur, $jour, 0, 2, 8, 9, 1));
            $resultat_travail = $request_travail->fetchAll(PDO::FETCH_ASSOC);

            // Stocking the results
            array_push($weekStat, $resultat_travail[0]);
        }
        foreach($weekStat as $key => $value){
            echo($total); // I get 7 at the end because $value is always = 1
            echo((int)$value); // I get 1 everytime
            $total = $total + (int)$value;
        }
        return $weekStat;
    }

Solution

  • When recording the individual results, you are adding the row of data each time. As the only value you use is sum_count, you can add this value directly instead

    array_push($weekStat, $resultat_travail[0]['sum_count']);
    

    You should then be able to use array_sum() instead of a loop to calculate the total...

    $total = array_sum($weekStat);
    

    You may also be able to do the sum in SQL instead, which may be worth looking into.