Jump to content

[SOLVED] Adding totals to table of data


Shamrox

Recommended Posts

Since you have to loop through all the records to display them anyway, I would just keep a running total as you loop through the records instead of doing another query using SUM(). But either would work.

 

while ($record = mysql_fetch_array($result)) {

 //Code to display record goes here
 $total = $total + $record['amount'];

}

//Code to sdisplay total goes here

Here is a section of code that i Have now. I was thinking of putting a row below the looped rows but wasn't sure how to deal with sum() outside of the sql statement.

 

while ($order = mysql_fetch_array($orders)) {
$orderdate = $order['orderdate'];
$lastname = $order['lastname'];
$firstname = $order['firstname'];
$course_num = $order['course_num'];
$ctitle = substr($order['ctitle'], 0,30);
$vendorname = $order['vendorname'];
$startdate = $order['startdate'];
$enddate = $order['enddate'];
$courselength = $order['courselength'];
$purchaseorder = $order['purchase_order'];
$costcenter = $order['cost_center'];
$listprice = $order['vendorlistprice'];
$salesprice = $order['sale_price'];
$savings = $order['vendorlistprice']-$order['sale_price'];
$cogs = $order['COGS'];
$profit = $order['sale_price']-$order['COGS'];
$status = $order['status'];
echo "<tr>\n";
echo "<td nowrap='nowrap'>$orderdate</td>\n";
echo "<td nowrap='nowrap'>$lastname, $firstname</td>\n";
echo "<td nowrap='nowrap'>$course_num</td>\n";
echo "<td nowrap='nowrap'>$ctitle</td>\n";
echo "<td nowrap='nowrap'>$vendorname</td>\n";
echo "<td nowrap='nowrap'>$startdate</td>\n";
echo "<td nowrap='nowrap'>$enddate</td>\n";
echo "<td nowrap='nowrap'>$courselength</td>\n";
echo "<td nowrap='nowrap'>$purchaseorder</td>\n";
echo "<td nowrap='nowrap'>$costcenter</td>\n";
echo "<td nowrap='nowrap'>$$listprice</td>\n";
echo "<td nowrap='nowrap'>$$salesprice</td>\n";
echo "<td nowrap='nowrap'>$$savings</td>\n";
echo "<td nowrap='nowrap'>$$cogs</td>\n";
echo "<td nowrap='nowrap'>$$profit</td>\n";
echo "<td nowrap='nowrap'>$status</td>\n";
echo "</tr>\n";
}

<?php
while ($order = mysql_fetch_array($orders)) {
$orderdate = $order['orderdate'];
$lastname = $order['lastname'];
$firstname = $order['firstname'];
$course_num = $order['course_num'];
$ctitle = substr($order['ctitle'], 0,30);
$vendorname = $order['vendorname'];
$startdate = $order['startdate'];
$enddate = $order['enddate'];
$courselength = $order['courselength'];
$purchaseorder = $order['purchase_order'];
$costcenter = $order['cost_center'];
$listprice = $order['vendorlistprice'];
$salesprice = $order['sale_price'];
$savings = $order['vendorlistprice']-$order['sale_price'];
$cogs = $order['COGS'];
$profit = $order['sale_price']-$order['COGS'];
$status = $order['status'];
echo "<tr>\n";
echo "<td nowrap='nowrap'>$orderdate</td>\n";
echo "<td nowrap='nowrap'>$lastname, $firstname</td>\n";
echo "<td nowrap='nowrap'>$course_num</td>\n";
echo "<td nowrap='nowrap'>$ctitle</td>\n";
echo "<td nowrap='nowrap'>$vendorname</td>\n";
echo "<td nowrap='nowrap'>$startdate</td>\n";
echo "<td nowrap='nowrap'>$enddate</td>\n";
echo "<td nowrap='nowrap'>$courselength</td>\n";
echo "<td nowrap='nowrap'>$purchaseorder</td>\n";
echo "<td nowrap='nowrap'>$costcenter</td>\n";
echo "<td nowrap='nowrap'>\$$listprice</td>\n";          // escape the "$" dollar sign
echo "<td nowrap='nowrap'>\$$salesprice</td>\n";
echo "<td nowrap='nowrap'>\$$savings</td>\n";
echo "<td nowrap='nowrap'>\$$cogs</td>\n";
echo "<td nowrap='nowrap'>\$$profit</td>\n";
echo "<td nowrap='nowrap'>$status</td>\n";
echo "</tr>\n";
$total_listprice += $listprice;
$total_salesprice += $salesprice;      //etc
}
echo "<tr><td colspan='10'>Totals</td><td>\$$total_listprice</td><td>\$$total_salesprice</td><td>etc</td>";
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.