If you are going to the trouble of using a MySQL database, you should reap the benefit of the SQL SUM GROUP BY clauses, to have MySQL do that hard work for you.
Your result will be another table, which I leave to you to display.
Thanks for everyone. I have got the solution, which is to return the total value to 0 then the total value can be obtained and there is no need to use MySQL Query.