Click here to Skip to main content
16,012,759 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table with a column of id auto increment and the second with random numbers from 1 to 5.

Like:
id | number
1     4
2     5
3     3
4     4
5     1
6     3
7     4
...   ...


the td looks like that
<td>1</td><td id="p4">4</td>
<td>2</td><td id="p5">5</td>

I want to sum all the same numbers to display it into my second table, like:
5 | 4 | 3 | 2 | 1
4  12   6   0   1


JavaScript
$(document).ready(function(){
var p1 = document.getElementById("p1").innerHTML;
// sum the number of p1 we have in to table
});

Any idea?
Posted
Updated 16-Apr-13 10:37am
v2
Comments
Sergey Alexandrovich Kryukov 16-Apr-13 15:56pm    
Why not doing it all in PHP on server side?
—SA
Yafa Su 16-Apr-13 16:05pm    
you mean with php and mysql?... the first table was all I can get with sql, I don't know if I can make it better.
Sergey Alexandrovich Kryukov 16-Apr-13 16:33pm    
What do you mean "better"? If you get the table from server side, fill in the sum you want as well...
—SA

1 solution

Even though this approach is not advisable, you should always peform required data manipulation at the database level only (using MySQL in your case). Search for GROUP BY clause and use of SUM() function in MySQL. The query will look something like -
SQL
SELECT SCORE, SUM(SCORE) TOTAL FROM (SELECT ID, SCORE FROM MYTABLE) GROUP BY SCORE;

Anyway, for the JavaScript solution, check this link -
http://jsbin.com/efacem/1/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900