0

I'm totally not good with arrays and I have a database query that return this:

 ID USER    TASK    Points  Time
1   admin   Accounting  1   -7
1   admin   Accounting  1   -15
1   admin   Accounting  1   -1146
1   admin   Accounting  1   -2
1   admin   Accounting  1   -3
2   Mike    Encoding    1   -7
2   Mike    Encoding    1   -55
2   Mike    Encoding    1   0
2   Mike    Encoding    1   -6
3   Adam    Printing    1   -5
3   Adam    Printing    1   -12
3   Adam    Printing    1   -7
3   Adam    Printing    1   -4
3   Adam    Printing    1   -8
3   Adam    Printing    1   -10

I've use a loop to count how many points each user have but I got a problem getting their average time which is (sum of time / count(time))

SO far my code is this:

while($row = mysqli_fetch_assoc($result))
        {

            $countCase[$row['TASK']] = isset($countCase[$row['TASK']]) ? $countCase[$row['TASK']] + 1 : 1;  

            $countUser[$row['TASK']][$row['USER']] = isset($countUser[$row['TASK']][$row['USER']]) ? $countUser[$row['TASK']][$row['USER']] + 1 : 1;                    
        }

it basically count all the occurences but having difficulty getting the average time also is this array good for inserting it into a database or better suggestion?

3
  • 2
    Or you can use a query that take advantage of mysql SUM(), COUNT() and GROUP BY Commented Mar 22, 2016 at 1:19
  • I'll try to do that , Thanks! Commented Mar 22, 2016 at 1:31
  • 1
    I guess (SUM(Time)/COUNT(ID)) AS TimeAverage with GROUP BY USER will work. Or with GROUP BY USER, TASK or GROUP BY TASK, USER. Whichever suits your case. Commented Mar 22, 2016 at 1:55

1 Answer 1

2

You can rewrite your query using group and MySQL functions to solve this question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.