Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript PHP jQuery
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
 
$(document).ready(function(){
var p1 = document.getElementById("p1").innerHTML;
// sum the number of p1 we have in to table
});
Any idea?
Posted 16-Apr-13 10:11am
Yafa Su303
Edited 16-Apr-13 11:37am
Maciej Los150.7K
v2
Comments
Sergey Alexandrovich Kryukov at 16-Apr-13 15:56pm
   
Why not doing it all in PHP on server side?
—SA
Yafa Su at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 -
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/[^]
  Permalink  

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100