kat35601 Posted November 14, 2014 Share Posted November 14, 2014 (edited) Hello I am very new to php and programming and I need a grand total of "$Total = odbc_result($result, "total");" but not sure of how to create it. $result =odbc_exec($connect,$sql); if(!$result){ exit("Error in SQL"); } echo "<table><tr>"; echo "<th>Entered</th>"; echo "<th>Orders</th>"; echo "<th>Total</th>"; while (odbc_fetch_row($result)) { $EnteredBy= odbc_result($result,"ompCreatedBy"); $Orders= odbc_result($result, "orders"); $Total = odbc_result($result, "total"); //Grand total of total needed echo "<tr><td>$EnteredBy</td>"; echo "<td>$Orders</td>"; echo "<td>$Total</td>"; echo "<td>$Grand_Total</td>"; } odbc_close($connect); ?> </body> </html> Edited November 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/ Share on other sites More sharing options...
ginerjm Posted November 14, 2014 Share Posted November 14, 2014 1 - you can build the total calc into your query statement. Show us your query 2 - you really should grab your results row by row instead of individually by column. It's a waste of resources the way you are doing it. Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496572 Share on other sites More sharing options...
kat35601 Posted November 14, 2014 Author Share Posted November 14, 2014 (edited) 1> $sql="select SO.ompCreatedBy, count(SO.ompSalesOrderID) as orders,round(sum(SO.ompOrderTotalBase),2) as total FROM m1_DC.dbo.SalesOrders SO Where SO.ompOrderDate=CONVERT(varchar,GETDATE(),101) Group by SO.ompCreatedBy"; 2> first way I got it to work Edited November 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496575 Share on other sites More sharing options...
ginerjm Posted November 14, 2014 Share Posted November 14, 2014 From your posted query the part that says " round(sum(SO.ompOrderTotalBase),2) as total " gives you a total of that column for each of the ompcreatedby values. How are you processing the output of your query? In a loop? Don't understand your next comment. Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496576 Share on other sites More sharing options...
kat35601 Posted November 14, 2014 Author Share Posted November 14, 2014 (edited) <?php $connect =odbc_connect("removed"); if(!$connect) { exit("Connection Failed: " . $connect); } $sql="select SO.ompCreatedBy, count(SO.ompSalesOrderID) as orders,round(sum(SO.ompOrderTotalBase),2) as total FROM m1_DC.dbo.SalesOrders SO Where SO.ompOrderDate=CONVERT(varchar,GETDATE(),101) Group by SO.ompCreatedBy " ; $result =odbc_exec($connect,$sql); if(!$result){ exit("Error in SQL"); } echo "<table><tr>"; echo "<th>Entered</th>"; echo "<th>Orders</th>"; echo "<th>Total</th>"; while (odbc_fetch_row($result)) { $EnteredBy= odbc_result($result,"ompCreatedBy"); $Orders= odbc_result($result, "orders"); $Total = odbc_result($result, "total"); echo "<tr><td>$EnteredBy</td>"; echo "<td>$Orders</td>"; echo "<td>$Total</td>"; } odbc_close($connect); ?> you 2 - you really should grab your results row by row instead of individually by column. It's a waste of resources the way you are doing it. me 2> first way I got it to work Edited November 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496577 Share on other sites More sharing options...
ginerjm Posted November 14, 2014 Share Posted November 14, 2014 Not familiar with the odbc extension but I would guess you get each column in that loop this way: while ($row = odbc_fetch_array($result)) { echo "<tr><td>" . $row['ompCreatedBy'] ."</td>"; echo "<td>" .$row['orders'] ."</td>"; echo "<td>" . $row['total'] ."</td></tr>"; $gr_total = $gr_total + $row['total']; } Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496579 Share on other sites More sharing options...
kat35601 Posted November 14, 2014 Author Share Posted November 14, 2014 (edited) Notice: Undefined variable: gr_total in /var/www/html/kk_orders_entered_by.php on line 37 Entered Orders Total EWOODY 16 3415.17 JDODSON 3 1509.01 REVANS 3 0.00 TMOTES 9 2760.57 $result =odbc_exec($connect,$sql); if(!$result){ exit("Error in SQL"); } echo "<table><tr>"; echo "<th>Entered</th>"; echo "<th>Orders</th>"; echo "<th>Total</th>"; //**************************************************************** while ($row = odbc_fetch_array($result)) { echo "<tr><td>" . $row['ompCreatedBy'] ."</td>"; echo "<td>" .$row['orders'] ."</td>"; echo "<td>" . $row['total'] ."</td></tr>"; $gr_total = $gr_total + $row['total']; } //****************************************************************** Edited November 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496581 Share on other sites More sharing options...
ginerjm Posted November 14, 2014 Share Posted November 14, 2014 My bad. You have to initialize the var before the loop Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496584 Share on other sites More sharing options...
Ch0cu3r Posted November 14, 2014 Share Posted November 14, 2014 Initialize $gr_total outside of the while loop $gr_total = 0; while ($row = odbc_fetch_array($result)) Also please wrap your code in tags or click the <> button in the editor. (I have edited your posts for you) Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496585 Share on other sites More sharing options...
kat35601 Posted November 14, 2014 Author Share Posted November 14, 2014 Works great Thanks Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496588 Share on other sites More sharing options...
ginerjm Posted November 14, 2014 Share Posted November 14, 2014 HTH! Quote Link to comment https://forums.phpfreaks.com/topic/292464-i-need-a-grand-total-of-total-odbc_resultresult-total/#findComment-1496612 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.