Calculate values for each column of an html table

Asked

Viewed 463 times

0

I want to calculate values from a table. Each column has different values, at the end of the table, I would like to inform the total value. Below is my html.

<table class="table table-striped m-table m-table--head-bg-success">
  <thead>
    <tr>
      <th>Nº doc</th>
      <th>Valor doc</th>
      <th>Correção doc</th>
      <th>Multa doc</th>
      <th>Valor extra doc</th>
      <th>Valor Total</th>
    </tr>
  </thead>
  <tbody>
    <?php 
      $sql = $db->prepare("SELECT * FROM doc where cod_doc = '$id_get' ORDER BY data ASC ");          
      $sql->execute();   $a = 0  ;        
           while($row=$sql->fetch(PDO:: FETCH_ASSOC)){ 
              $a++;
        $pct_multa = 2;
        $pct_ext = 1;
        $pct_cob = 10; 
        $valor_doc = $row['valor_doc'];
        $data = $row['data'];     
    ?>
    <tr>
      <th scope="row">
        <? echo $row['numero_doc']?>
      </th>

      <td id="sum1"> 
        <? echo str_replace('.', ',', $valor_doc)?>
      </td>
      <td id="sum2">
        <? echo str_replace('.', ',', $doc_correcao)?>
      </td>
      <td id="sum3">
        <? echo valorMulta($valor_doc,$pct_multa) ?>
      </td>
      <td id="sum4">
        <? echo valorext($valor_doc,$pct_ext) ?>
      </td>
    
      <td id="sum5"><span class="m-widget6__text m--align-right m--font-boldest m--font-brand">
														<? echo valorTotal($valor_doc,$pct_ext,$pct_multa, $data) ?>
													</span></td>
    </tr>
    <?php } ?>
  </tbody>
</table>

I created an id for each td (id="sum1") total of the column I want to calculate. How can I proceed ? Thanks for your valuable attention.

  • 1

    vc want to calculate using php or js?

  • Js .. I will create a table at the end with the sum of each column. For visualization only .

  • The table already has the correct information. It just doesn’t do the calculation at the end . Because I don’t know how to do.

  • 1

    Are you using Jquery? Or pure js?

  • Utilizo jQuery .

1 answer

1


First of all, the attribute id is something that should be unique, so instead of id use class:

<tr>
    <th scope="row">
    <? echo $row['numero_doc']?>
    </th>
    <td class="sum1">
        <? echo str_replace('.', ',', $valor_doc)?>
    </td>
    <td class="sum2">
        <? echo str_replace('.', ',', $doc_correcao)?>
    </td>
    <td class="sum3">
        <? echo valorMulta($valor_doc,$pct_multa) ?>
    </td>
    <td class="sum4">
        <? echo valorext($valor_doc,$pct_ext) ?>
    </td>    
    <td class="sum5">
        <span class="m-widget6__text m--align-right m--font-boldest m--font-brand">
            <? echo valorTotal($valor_doc,$pct_ext,$pct_multa, $data) ?>
        </span>
    </td>
</tr>

In the Jquery you can repeat the example for each column:

var sum1 = 0;

$.each($('.sum1'),function(){
    var num = parseInt($(this).text().replace(/[^0-9]/g,''));
    sum1 += num;
});
  • .replace(/[^0-9]/g,'') keeps only the numbers of text broken by text();

  • parseInt() transforms the string in integer;

Then just reform the value to monetary and include where it will be displayed.

  • 1

    Perfect. I will perform a test now. Thank you!

  • Wees Smith, did not work out very well. td sum1 appears 10 times. will that be the problem ?

  • Disregard my message above. I made a console.log in variable. is calculating correctly.

  • 1

    yes, it will add up all the tds q have the sum1 class, etc

Browser other questions tagged

You are not signed in. Login or sign up in order to post.