Click here to Skip to main content
11,709,955 members (80,354 online)
Rate this: bad
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.

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

var p1 = document.getElementById("p1").innerHTML;
// sum the number of p1 we have in to table
Any idea?
Posted 16-Apr-13 9:11am
Yafa Su253
Edited 16-Apr-13 10:37am
Maciej Los194.1K
Sergey Alexandrovich Kryukov at 16-Apr-13 15:56pm
Why not doing it all in PHP on server side?
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...

1 solution

Rate this: bad
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 -
Anyway, for the JavaScript solution, check this link -[^]

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 524
1 OriginalGriff 245
2 Peter Leow 194
3 Abhinav S 102
4 Suvendu Shekhar Giri 95
0 OriginalGriff 9,428
1 Sergey Alexandrovich Kryukov 9,037
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2015
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