1
I have a table that is automatically generated by PHP and manipulated by jquery, only I need to calculate the percentage of the total of each of the numerical lines, what is the best way to do this?
<div id="tableResults" class="container">
<table id="table_resultados" class="table table-bordered table-hover">
<thead class="table-head">
<tr>
<th>SEX</th>
<th class="names">FATAL</th>
<th class="names">SERIOUS</th>
<th class="names">OTHERS</th>
<th class="names">TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>WOMAN</td>
<td class="VitimaFATAL">4</td>
<td class="SERIOUS">6</td>
<td class="OTHERS">1</td>
<td class="Total">11</td>
</tr>
<tr>
<td>MEN</td>
<td class="FATAL">18</td>
<td class="SERIOUS">28</td>
<td class="OTHERS">23</td>
<td class="Total">69</td>
</tr>
<tr>
<td>UNKNOWN</td>
<td class="FATAL">3</td>
<td class="SERIOUS">1679</td>
<td class="OTHERS">3129</td>
<td class="Total">4811</td>
</tr>
<tr id="Totais">
<td>TOTAL</td>
<td id="FATAL">25</td>
<td id="SERIOUS">1713</td>
<td id="OTHERS">3153</td>
<td id="Total">4891</td>
</tr>
</tbody>
</table>
</div>
This table comes from a dynamic search in PHP and MYSQL that generates the values, I already made a function in jQuery to calculate the total
function colSum() {
var ids = new Array();
$('#table_resultados .names').each(function (){
ids.push($(this).html().replace(/\s/g, ''));
$('#Totais').append('<td id="' + $(this).html().replace(/\s/g, '') + '"></td>')
});
$.each(ids, function (index, value) {
var sum = 0;
$('.' + value).each(function () {
sum += parseInt($(this).html());
});
$('#' + value).html(sum);
});
I need to calculate the percentage of the total of all columns. In this example I need to insert a column after the FATAL, SERIOUS, OUTHERS % of total and with the values, example:
- the column % of the total FATAL would be 36,36 - 26,08 - 0,06 - 0,51
- the column % of the total SERIOUS would be 54,54 - 40,57 - 34,89 - 35,02
The account to be made is the value of the column multiplied by 100 divided by the value of the total of the same example row of the first row 4*100/11
I created an HTML to look more or less like I need, I don’t know how to generate it dynamically for all lines
<div id="tableResults" class="container">
<table id="table_resultados" class="table table-bordered table-hover">
<thead class="table-head">
<tr>
<th>SEX</th>
<th class="names">FATAL</th>
<th>% do TOTAL</th>
<th class="names">SERIOUS</th>
<th class="names">OTHERS</th>
<th class="names">TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>WOMAN</td>
<td class="VitimaFATAL">4</td>
<th>36,36</th>
<td class="SERIOUS">6</td>
<td class="OTHERS">1</td>
<td class="Total">11</td>
</tr>
<tr>
<td>MEN</td>
<td class="FATAL">18</td>
<th>26,08</th>
<td class="SERIOUS">28</td>
<td class="OTHERS">23</td>
<td class="Total">69</td>
</tr>
<tr>
<td>UNKNOWN</td>
<td class="FATAL">3</td>
<th>0,06</th>
<td class="SERIOUS">1679</td>
<td class="OTHERS">3129</td>
<td class="Total">4811</td>
</tr>
<tr id="Totais">
<td>TOTAL</td>
<td id="FATAL">25</td>
<th>0,51</th>
<td id="SERIOUS">1713</td>
<td id="OTHERS">3153</td>
<td id="Total">4891</td>
</tr>
</tbody>
</table>
</div>
You can’t put the HTML already with the columns % of the total, it’s a bit confusing, at least for me, understand.
– user60252
Explain to me something, where you got these figures?
a coluna % do total do FATAL seria 36,36 - 26,08 - 0,06 - 0,51
anda coluna % do total do SERIOUS seria 54,54 - 40,57 - 34,89 - 35,02
. I was able to understand that you wanted to take the values that are in the columns and group by the classes:FATAL,SERIOUS,OTHERS
;– Marconi
@Marconi I did on the calculator
– Guilherme Freire
@Guilhermefreire what is the calculation to arrive at these percentages? Could edit your question and add something else?
– Marconi
@Marconi The account to be made is the value of the column multiplied by 100 divided by the value of the total of the same row example of the first row 4*100/11 so I do not know how to do this dynamically by jquery on all rows
– Guilherme Freire
@Guilhermefreire I saw your edition got much better, I’ll have lunch at the moment, so I arrive and do not have an answer help you.
– Marconi
@Leocaracciolo do not know what to do with jquery to get these values I already use php q does a lot of stuff with mysql, I thought q would be easier, post your reply in php for me to take a look, maybe it will be simpler even
– Guilherme Freire
@Leocaracciolo to using Pdo
– Guilherme Freire
It’s there in the answer in php Pdo
– user60252