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

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

<?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>";
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.