Mind blank....PHP Query

  • Thread starter Thread starter HC-Martin
  • Start date Start date
H

HC-Martin

Been thinking about this for way too long now, so thought I'd see if any of you good people are able to solve this.....

PHP:
$sql = "SELECT * FROM tbl_orderitems INNER JOIN tbl_products ON tbl_orderitems.ProductID=tbl_products.ProductID WHERE tbl_orderitems.OrderID=".$oid." ORDER BY tbl_orderitems.ItemID ASC";
mysql_select_db("database", $con);//connecting mysql db function i wrote
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
 echo "<div class='item'>".$row['Name']."</div>";
 echo "<div class='price'>£".$row['Price']."</div>";
 echo "<div class='qty'>".$row['Qty']."</div>";
$totalamount=$row['Price']*$row['Qty'];
echo "<div class='total'>£".$totalamount."</div><br />";
$total=sum($row['Price']);
}
echo $total;

Basically I'm listing items in the order and then want to be able to calculate the total order price, by adding $totalamount for each row.

There's probably a really simple and easy way to do it....but I just can't work it out, and now getting fed up.....:|

Thanks
 
Hi

Or a short version would be :

$total+ = $totalamount;

That would add the totalamount to total for each record.

I hope this helps.

Many Thanks

Leo
 
Upvote 0
If you wish to optimise the code, I would suggest removing the echo statements, place the output in a string then print it in one go at the end. Something like this.
PHP:
$output .= "<div class='item'>".$row['Name']."</div>";
$output .= "<div class='price'>£".$row['Price']."</div>";
$output .= "<div class='qty'>".$row['Qty']."</div>";
........
 
echo $output;
This method will parse faster than printing each line individually.
 
Upvote 0
If you wish to optimise the code, I would suggest removing the echo statements, place the output in a string then print it in one go at the end. Something like this.
PHP:
$output .= "<div class='item'>".$row['Name']."</div>";
$output .= "<div class='price'>£".$row['Price']."</div>";
$output .= "<div class='qty'>".$row['Qty']."</div>";
........
 
echo $output;
This method will parse faster than printing each line individually.

Thanks. It's only a small scale admin database, but i'll bear that in mind for larger things I will be doing. :)
 
Upvote 0

Latest Articles