Shamrox Posted August 9, 2007 Share Posted August 9, 2007 I have a table of data that is generated with a query php/mysql. I'd like to show a total for one of the columns that include dollar amounts. How would i go about doing that? Link to comment https://forums.phpfreaks.com/topic/64124-solved-adding-totals-to-table-of-data/ Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 What gave you got coded so far? Link to comment https://forums.phpfreaks.com/topic/64124-solved-adding-totals-to-table-of-data/#findComment-319565 Share on other sites More sharing options...
frost Posted August 9, 2007 Share Posted August 9, 2007 There is a nice tutorial about sum(); here - http://www.tizag.com/mysqlTutorial/mysqlsum.php - that might be what you are looking for? If your columns have the $ in them already you might have to strip it off to get sum(); to work? Link to comment https://forums.phpfreaks.com/topic/64124-solved-adding-totals-to-table-of-data/#findComment-319570 Share on other sites More sharing options...
Psycho Posted August 9, 2007 Share Posted August 9, 2007 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 https://forums.phpfreaks.com/topic/64124-solved-adding-totals-to-table-of-data/#findComment-319572 Share on other sites More sharing options...
Shamrox Posted August 9, 2007 Author Share Posted August 9, 2007 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 https://forums.phpfreaks.com/topic/64124-solved-adding-totals-to-table-of-data/#findComment-319575 Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 <?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 https://forums.phpfreaks.com/topic/64124-solved-adding-totals-to-table-of-data/#findComment-319594 Share on other sites More sharing options...
Shamrox Posted August 9, 2007 Author Share Posted August 9, 2007 Sweet. Thanks Barand. Gold star for you if I could give it! Link to comment https://forums.phpfreaks.com/topic/64124-solved-adding-totals-to-table-of-data/#findComment-319596 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.