Adding totals of values given in dynamic rows populated from database

Adding totals of values given in dynamic rows populated from database

I have a dynamic table, where Column A is populated dynamically from the database and one column is an input fields for ratings. The code is in the link below : 

Click here to view the code

What I am trying to do :

All the data elements should add up and give the sub-total in the Sub-Total field present on top of the fields.
The sub-totals should then add up and and give a grand total. I've attached a photo for better understanding.



NB : The sub-total fields are dynamic, and can change the row number depending on the database values.

My issue :

When I am inputing values instead of going to their respective subtotals, they add up all together. Where have I gone wrong? Appreciate any suggestions..