Result 'NaN' when I add values

1

I have a script with which I need to add the values of the columns, but the result appears as NaN. I need to add from the second column to the sixth. Are you taking values that are not numerical?

This is the script:

<script>
       var totals=[0,0,0,0,0];
        $(document).ready(function(){
            var $dataRows=$("#mytable tr:not('.totalColumn, .titlerow')");
            $dataRows.each(function() {
                $(this).find('.rowDataSd').each(function(i){        
                    totals[i]+=parseInt( $(this).html());
                });
            });
            $("#mytable td.totalCol").each(function(i){  
                $(this).html(totals[i]);
            });
        });
</script>

This is PHP code:

<table id="mytable">
 <thead>
 <tr class="titlerow">
 <th scope="col">Column1</th>
 <th>Column2</th>
 <th>Column3</th>
 <th>Column4</th>
 <th>Column5</th>
 <th>Column6</th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <?php
 include("conn.php");

$result = mysql_query("SELECT * FROM table1 GROUP BY name");

 while($test = mysql_fetch_array($result))
 {
 $id = $test['id']; 
 echo"<td>".$test['name']."</td>";
 echo"<td class='rowDataSd'>".$test['value1']."</td>";
 echo"<td class='rowDataSd'>".$test['value2']."</td>";
 echo"<td class='rowDataSd'>".$test['value3']."</td>";
 echo"<td class='rowDataSd'>".$test['value4']."</td>";
 echo"<td class='rowDataSd'>".$test['value5']."</td>";
 echo "</tr>";
 }
 mysql_close($conn);
 echo '<tfoot>
    <tr class="totalColumn">
        <td>.</td> 
        <td class="totalCol">Total:</td> 
        <td class="totalCol">Total:</td> 
        <td class="totalCol">Total:</td> 
        <td class="totalCol">Total:</td> 
        <td class="totalCol">Total:</td> 
    </tr>
</tfoot>';
 ?>
</table>
    
asked by Guif If 06.12.2018 в 21:56
source

1 answer

1

I recommend that you calculate the total values directly in PHP, it is not necessary to use JQuery to do this calculation.

The code would be something like this:

<table id="mytable">
 <thead>
 <tr class="titlerow">
 <th scope="col">Column1</th>
 <th>Column2</th>
 <th>Column3</th>
 <th>Column4</th>
 <th>Column5</th>
 <th>Column6</th>
 </tr>
 </thead>
 <tbody>
 <?php
 include("conn.php");

$result = mysql_query("SELECT * FROM table1 GROUP BY name");
$col1 = 0;
$col2 = 0;
$col3 = 0;
$col4 = 0;
$col5 = 0;

 while($test = mysql_fetch_array($result))
 {
 $id = $test['id']; 
 echo"<tr><td>".$test['name']."</td>";
 echo"<td class='rowDataSd'>".$test['value1']."</td>";
 echo"<td class='rowDataSd'>".$test['value2']."</td>";
 echo"<td class='rowDataSd'>".$test['value3']."</td>";
 echo"<td class='rowDataSd'>".$test['value4']."</td>";
 echo"<td class='rowDataSd'>".$test['value5']."</td>";
 echo "</tr>";

  $col1 += $test['value1'];
  $col2 += $test['value2'];
  $col3 += $test['value3'];
  $col4 += $test['value4'];
  $col5 += $test['value5'];
 }
 mysql_close($conn);
 echo '<tfoot>
    <tr class="totalColumn">
        <td>Total:</td> 
        <td class="totalCol">'.$col1.'</td> 
        <td class="totalCol">'.$col2.'</td> 
        <td class="totalCol">'.$col3.'</td> 
        <td class="totalCol">'.$col4.'</td> 
        <td class="totalCol">'.$col5.'</td> 
    </tr>
</tfoot>';
 ?>
</table>
    
answered by 07.12.2018 / 17:05
source