why doesn't this code sum my spent_hours for each user?
while ($row = $result->fetch_assoc())
{
$total_spent_time += $row['spent_time'];
if (!array_key_exists($row['activity_type'], $data))
{
$data[$row['activity_type']] = array(
'spent_time' => array('user' => array())
);
$data[$row['activity_type']]['spent_time']['user'][$row['user']] = array('time' => $row['spent_time']);
if (array_key_exists($row['user'], $data[$row['activity_type']]['spent_time']['user']))
{
$data[$row['activity_type']]['spent_time']['user'][$row['user']]['time'] += $row['spent_time'];
}
}
It gives me only the last value of the row.
Try to declare counter before loop:
$total_spent_time = 0;
while ($row = $result->fetch_assoc()) {
$total_spent_time += $row['spent_time'];
}
echo $total_spent_time;
You are doing wrong some where, we need some more details of query and resultset.
You are not using $total_spent_time anywhere in the array allocation.
$data[$row['activity_type']]['spent_time']['user'][$row['user']] = array('time' => $row['spent_time']);
as you are only using $row['spent_time'], and since there is no second loop if statement executs only once per user($row['user'])
That's the reason you get only 1 value.