Skip to content Skip to sidebar Skip to footer

Sum Column Values In Table

I am trying to sum the values of the 'Total Work Assigned' and 'Not Called' columns. Here is my table HTML
Use

Solution 1:

I guess what you want to do is something like this:

DEMO: https://jsfiddle.net/zxooa1j4/1/

var sum1 = 0;
var sum2 = 0;
$("#category tr").not(':first').not(':last').each(function() {
  sum1 +=  getnum($(this).find("td:eq(2)").text());
  sum2 +=  getnum($(this).find("td:eq(3)").text());
  functiongetnum(t){
    if(isNumeric(t)){
        returnparseInt(t,10);
    }
    return0;
    functionisNumeric(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
  }
});
$("#sum1").text(sum1);
$("#sum2").text(sum2);

isNumeric() function is quoted from this answer:

Is there any function like IsNumeric in javascript to validate numbers

HTML:

<tableborder=1id="category"><tr><th>User Name</th><th>User Belongs</th><th>Total Work Assigned</th><th>Not Called</th></tr><tr><td>vidyaranyapura</td><td>Category</td><td>172</td><td>156</td></tr><tr><td>sahasra</td><td>Company</td><td>500</td><td>350</td></tr><tr><td>global</td><td>Not Assigned</td><td>0</td><td>0</td></tr><tr><td></td><td></td><tdid="sum1"></td><tdid="sum2"></td></tr></table>

Hope this helps.

Solution 2:

Based on the PHP that you provided in your fiddle. You can actually total the values while you're looping through them for output to the display.

Simply replace this foreach section

$table2 = '<tableid="all_category_data"class="table table-striped table-bordered table-hover"><tr><th>User Name</th><th>User Belongs to</th><th>Total Work Assigned</th><th>Not Called</th><th>Follow Up</th><th>Intrested</th><th>Not Intrested</th></tr>';

foreach($totalresult as $totalresults) {
    $table2 .= "<tr><td>".$totalresults['username'].
    "</td><td>".$totalresults['userbelongs'].
    "</td><td>".$totalresults['totalvalue'].
    "</td><td>".$totalresults['Notcalled'].
    "</td><td>".$totalresults['followUp'].
    "</td><td>".$totalresults['intrested'].
    "</td><td>".$totalresults['notIntrested'].
    "</td></tr>";
}

$table2. = '</table>';

echo $table2;

with the following that includes the addition and output

$table2 = '<table  id="all_category_data" class="table table-striped table-bordered table-hover"><tr><th>User Name</th><th>User Belongs to</th><th>Total Work Assigned</th><th>Not Called</th><th>Follow Up</th><th>Intrested</th><th>Not Intrested</th></tr>';
$totalValue = 0;
$notCalledValue = 0;
foreach($totalresultas$totalresults) {
    // addition of totalValue$totalValue = $totalValue + $totalresults['totalvalue'];
    // addition of notCalledValue$notCalledValue = $notCalledValue + $totalresults['Notcalled'];

    $table2 .= "<tr><td>".$totalresults['username'].
    "</td><td>".$totalresults['userbelongs'].
    "</td><td>".$totalresults['totalvalue'].
    "</td><td>".$totalresults['Notcalled'].
    "</td><td>".$totalresults['followUp'].
    "</td><td>".$totalresults['intrested'].
    "</td><td>".$totalresults['notIntrested'].
    "</td></tr>";
}
// output of totalValue and notCalledValue$table2 .= "<tr><td>".
    "</td><td>".
    "</td><td>".$totalValue.
    "</td><td>".$notCalledValue.
    "</td><td>".
    "</td><td>".
    "</td><td>".
    "</td></tr>";

$table2. = '</table>';
echo$table2;

NOTE: this assumes that the $totalresults['totalvalue'] and $totalresults['Notcalled'] keys in your data are numbers.

Post a Comment for "Sum Column Values In Table"