Jump to content

Recommended Posts

Hi,

 

I have a mysql_query within PHP that multiplies the price of a product by the quantity bought of that product to give a subtotal.

 

products.product_price * order_prod.order_quan AS product_subtotal

 

This works fine and is called upon using

 

($get_data['product_subtotal']

 

As you would expect.

 

 

My only problem is that I need to get a total order cost.

 

I thought that it would merely be a case of doing something like

SUM(product_subtotal) AS total_order_price

 

But this brings back nothing.

 

I know this must be very simple as it's only adding together a few values, but I've been stuck on this same problem for hours and time is running out ahhh

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/236634-finding-the-sum-of-a-sum/
Share on other sites

          $sql = "SELECT         products.product_id, 
				   products.product_price, 
				   order_prod.order_quan, 
				   products.product_price * order_prod.order_quan AS product_subtotal"."
				   FROM products, order_prod"." 
				   WHERE products.product_id = order_prod.product_id "." 
				   AND order_prod.order_id = " . $_GET["order_id"]." 
				   ORDER BY products.product_id";
		$get = mysql_query($sql);
	if(mysql_num_rows($get)==0){
		echo "Sorry but there is nothing available.";
		}else{
			while ($get_data = mysql_fetch_assoc($get)){
				echo $get_data['product_subtotal'];

 

Some more code as requested, I hope this helps!

$sql = "SELECT         products.product_id, 
				   products.product_price, 
				   order_prod.order_quan, 
				   products.product_price * order_prod.order_quan AS product_subtotal,
                                           SUM(product_subtotal) AS total_order_price"."----------------------------------sum line which doesn't work
				   FROM products, order_prod"." 
				   WHERE products.product_id = order_prod.product_id "." 
				   AND order_prod.order_id = " . $_GET["order_id"]." 
				   ORDER BY products.product_id";
		$get = mysql_query($sql);
	if(mysql_num_rows($get)==0){
		echo "Sorry but there is nothing available.";
		}else{
			while ($get_data = mysql_fetch_assoc($get)){
				echo $get_data['product_subtotal'];
                                        echo $get_data['total_order_price'];-----------------------------total order price echoed here

 

 

I just want to add up the values of the subtotals of each product.

 

So my product_subtotal would be multiplying the quantity of the product by the price of the product.

 

I then want to add all of those quantity subtotals together to give an order total.

 

So sample output,

 

Product name       Quantity Product price Subtotal

product name1 1         £350.00         £350.00

product name2 4         £70.00         £280.00

 

Order total price

£630.00

 

I just want to add together the subtotals to get a total order price.

 

Sorry if my explanation isn't great; my brain has been used and abused this past few weeks.

 

 

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.