如何对计算的表列求和

时间:2012-11-18 19:40:48

标签: php javascript mysql joomla

我需要在显示的表格中汇总一列。 我的问题: 如何汇总已从其他列计算数据的表列? 我需要使用javascript来做吗? 我想在相关列的底部显示总计。 提前感谢您的建议。

我当前计算数据的示例:

echo round(($r->price_sold - $r->shipping - $r->tax - $r->fees) * $r->commission_rate / 100,2);



    $query = "
SELECT *
  FROM jos_data_Used_Gear
  ORDER BY client_name ASC  
  LIMIT 0, 100;
";

$db->setQuery($query);
$url = $db->loadObjectList();


echo '<table id="consignment-table" border="0" align="center" width="3000px">'; 
echo '<tr class="consignment-header"><th class="consignment-client">Client</th>'; 
echo '<th class="consignment-title">Item Name</th>'; 
echo '<th class="consignment-saleprice">Price Sold</th>'; 
echo '<th class="consignment-saleprice">Tax</th>';
echo '<th class="consignment-saleprice">Shipping</th>';
echo '<th class="consignment-saleprice">Service Fee</th>';
echo '<th class="consignment-saleprice">Commission</th>';
echo '<th class="consignment-saleprice">Due Client</th>'; 

if(count($url)) {
  foreach($url as $r) {
    echo '<tr><td nowrap="nowrap">';
    echo $r->client_name;
    echo '</td><td nowrap="nowrap">'; 
    echo $r->item_title;
    echo '</td><td class="currency" nowrap="nowrap">'; 
    echo "&#36;";
    echo $r->price_sold;
    echo '</td><td nowrap="nowrap">'; 
    echo "&#36;";
    echo $r->tax;
    echo '</td><td class="currency" nowrap="nowrap">'; 
    echo "&#36;";
    echo $r->shipping;
    echo '</td><td nowrap="nowrap">'; 
    echo "&#36;";
    echo $r->fees;
    echo '</td><td nowrap="nowrap">'; 
    echo round(($r->price_sold - $r->shipping - $r->tax - $r->fees) * $r->commission_rate / 100,2);
    echo '</td><td class="currency" nowrap="nowrap">'; 
    echo "&#36;";
    echo round(($r->price_sold - $r->fees - $r->tax - $r->shipping) - (($r->price_sold - $r->fees - $r->tax - $r->shipping) * $r->commission_rate / 100) ,2);
    echo "</td></tr>";

  } 
}

echo '</table>';

1 个答案:

答案 0 :(得分:0)

http://jsbin.com/opevet/1/edit

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<table id="consignment-table" border="1" align="center" width="500px">
<tr class="consignment-header"><th class="consignment-client">Client</th>
<th class="consignment-title">Item Name</th>
<th class="consignment-saleprice">Price Sold</th>
<th class="consignment-saleprice">Tax</th>
<th class="consignment-saleprice">Shipping</th>
<th class="consignment-saleprice">Service Fee</th>
<th class="consignment-saleprice">Commission</th>
<th class="consignment-saleprice">Due Client</th>
    <tr><td nowrap="nowrap">client a
        </td><td nowrap="nowrap">1
        </td><td class="currency PriceSold" nowrap="nowrap">
            &#36;25.75
        </td><td nowrap="nowrap">0
        </td><td class="currency" nowrap="nowrap">0
        </td><td nowrap="nowrap">0
        </td><td nowrap="nowrap">0
        </td><td class="currency" nowrap="nowrap">0
    </td></tr>
    <tr><td nowrap="nowrap">client b
        </td><td nowrap="nowrap">2
        </td><td class="currency PriceSold" nowrap="nowrap">
            &#36;31.44
        </td><td nowrap="nowrap">0
        </td><td class="currency" nowrap="nowrap">0
        </td><td nowrap="nowrap">0
        </td><td nowrap="nowrap">0
        </td><td class="currency" nowrap="nowrap">0
    </td></tr>
    <tr><td nowrap="nowrap">client a
        </td><td nowrap="nowrap">3
        </td><td class="currency PriceSold" nowrap="nowrap">
            &#36;15.92
        </td><td nowrap="nowrap">0
        </td><td class="currency" nowrap="nowrap">0
        </td><td nowrap="nowrap">0
        </td><td nowrap="nowrap">0
        </td><td class="currency" nowrap="nowrap">0
    </td></tr>
    <tr><td nowrap="nowrap">total:
        </td><td nowrap="nowrap">
        </td><td class="currency total1" nowrap="nowrap">
        </td><td nowrap="nowrap">
        </td><td class="currency" nowrap="nowrap">
        </td><td nowrap="nowrap">
        </td><td nowrap="nowrap">
        </td><td class="currency" nowrap="nowrap">
    </td></tr>
</table>

<script>
$(document).ready(function(){
  var total1 = 0.0;
  $('.PriceSold').each(function(){
    try{
      total1 += $(this).html().replace('$','') * 1;
    } catch(ex){}
  });
  $('.total1').html('<b>$' + total1+ '</b>');
});
</script>  

</body>
</html>