McLaren
McLaren

Reputation: 776

Summing values from database

I don't know how to sum values from database. I have a lot of task ids in my array $alltas. In database I need to sum all these tasks hours. I've tried doing this:

foreach ($alltas as $keyy => $values) {
    $totalh = mysql_query("SELECT SUM(task_log_hours) AS hours FROM dotp_task_log WHERE task_log_task = '$values'");
    $totalh = mysql_fetch_array($totalh);
} 

$values is the id of task. Problem is that this script sums only for the same id. I need to sum all ids which array $alltas has. Is it possible? Database looks like:

task_id | task_hours
300     | 0.30
300     | 2.30
310     | 1

SO it should sum like 3.60. With my code it sums only with same id so 2.60. Please help

Upvotes: 0

Views: 79

Answers (1)

Rachel Geller
Rachel Geller

Reputation: 322

change the query into this

"select sum(task_log_hours) as hours from dotp_task_log where task_id in (" . join(',',$alltas) . ")"; 

and this won't need any foreach.

Upvotes: 4

Related Questions