bigrossco Posted January 9, 2007 Share Posted January 9, 2007 Not sure if their is the right place to put this.I have a php page and on it it has results from a database and I am wanting it to show the total cost (qnt * price) and then I also want it to add all the total prices together to give a total pricehow can I do this?Thanks for your help in advanceRoss Link to comment https://forums.phpfreaks.com/topic/33446-solved-calculate-total-cost/ Share on other sites More sharing options...
taith Posted January 9, 2007 Share Posted January 9, 2007 if you want more, you'll need to say how you got your database set up...[code]$result=mysql_query("SELECT * FROM table");while($row=mysql_fetch_array($result)){ $total=$total+$row[cost];}[/code] Link to comment https://forums.phpfreaks.com/topic/33446-solved-calculate-total-cost/#findComment-156578 Share on other sites More sharing options...
bigrossco Posted January 9, 2007 Author Share Posted January 9, 2007 this is how I have coded it:[code] <?php $host = "$lang_dbhost"; $user = "$lang_dbuser"; $pass = "$lang_dbpass"; $db = "$lang_dbase"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); $query = "SELECT DATE_FORMAT(date,'%d/%m/%Y'), price1, item1, paid, invo_no, item2, price2, qnt1, qnt2FROM client_invoices WHERE invo_no = '".$_SESSION["invoice"]."'"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());?><form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <?phpif (mysql_num_rows($result) > 0) { echo"<table border = 0>"; echo"<tr>"; while($row = mysql_fetch_row($result)){ echo"<tr>";echo"<table border = 1>";echo"<td><b><center>Qty</b></td><td><b><center>Description</b></td><td><b><center>Unit Price</b></td><td><b><center>TOTAL</b></td>";echo"<tr>";echo "<td>".$row[7]."</td>";echo "<td>".$row[2]."</td>";echo "<td>".$row[1]."</td>";echo"</tr>";echo "<td>".$row[8]."</td>";echo "<td>".$row[5]."</td>";echo "<td>".$row[6]."</td>"; } }?>[/code]I want $row[7] (quantity) to times by $row[1] (unit price)Ross Link to comment https://forums.phpfreaks.com/topic/33446-solved-calculate-total-cost/#findComment-156583 Share on other sites More sharing options...
bigrossco Posted January 9, 2007 Author Share Posted January 9, 2007 got it working, thanks Link to comment https://forums.phpfreaks.com/topic/33446-solved-calculate-total-cost/#findComment-156595 Share on other sites More sharing options...
bigrossco Posted January 9, 2007 Author Share Posted January 9, 2007 with the code given ;[code]$result=mysql_query("SELECT * FROM table");while($row=mysql_fetch_array($result)){ $total=$total+$row[cost];}[/code]Is their anyway i can have it rounded to it shows to the decimal point i.e. 50.00 insted of 50 Link to comment https://forums.phpfreaks.com/topic/33446-solved-calculate-total-cost/#findComment-156643 Share on other sites More sharing options...
taith Posted January 9, 2007 Share Posted January 9, 2007 take a look at http://us2.php.net/manual/en/function.number-format.php that'd help ya out there :-) Link to comment https://forums.phpfreaks.com/topic/33446-solved-calculate-total-cost/#findComment-156651 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.