Jump to content

[SOLVED] WHILE ERROR....


wmguk

Recommended Posts

Hey,

 

I have a script for a shopping cart im building.. its getting there now, basically i'm now trying to put the order details in tables and calculate them...

 

TABLE : ORDERS_DETAIL

 

ID | PRODNAME | PRODCODE | PRODPRICE | PRODQTY

5 | TEST ITEM | TEST0010 | 200   | 2

5 | TEST ID   | TEST0014 | 100   | 4

5 | DIFF ITEM | TEST0017 | 300   | 5

 

so total for order 5 is (2 x 200) + (4 x 100) + (5 x 300)

 

subtotal is 2300

 

TABLE : ORDERS

 

ID | NAME | SUBTOTAL | DEL | TOTAL

5  | JOHN  |  2300        | 40  |  2340

 

my code currently works out the subtotal for each row, not the whole order...

 

what i need to do is say, whats the total for the first row, what the total for the second row etc etc etc and add them together...

 

How can I do that?

 

this is my code:

if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
		foreach ($items as $item) 	{
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
									}

		foreach ($contents as $id=>$qty) {
			$productsql = "SELECT * FROM products WHERE code = '$id'";
			$productresult = mysql_query($productsql);
			while($prod = mysql_fetch_array($productresult)) 
			{
			$pcode = $prod['code'];
			$pname = $prod['name'];
			$pprice = $prod['price'];
			}

			//FIND SUBTOTAL FOR EACH ITEM
			$subt = $pprice * $qty;

	//INSERT DETAILS - MULTI LINE RESULT		
	$ins2 =  "INSERT INTO orders_details (order_id, prodname, prodcode, prodprice, prodqty)
				VALUES ('$ordid', '$pname', '$pcode', '$pprice', '$qty')";
	mysql_query($ins2);

		}

	//INSERT ORDER - SINGLE LINE RESULT	
$ins = 	"INSERT INTO orders (order_id, name, add1, add2, town, county, postcode, subtotal, delivery, total, ponum)
		VALUES ('$ordid', '$name', '$add1', '$add2', '$town', '$county', '$postcode', '$subt', '$del', '$tot', '$ponum')";
mysql_query($ins);

		}

Link to comment
https://forums.phpfreaks.com/topic/150287-solved-while-error/
Share on other sites

any thoughts on this?

 

i know what i need to do, just not how to do it..

 

i think i need to run a while there is a product with a qty then = the item subtotal...

while there is an item subtotal add them together = order subtotal....

 

but i just cant see how to run it...

Link to comment
https://forums.phpfreaks.com/topic/150287-solved-while-error/#findComment-789304
Share on other sites

i guess the easiest way would be to get all the products of a particular ID

 

SELECT * FROM ORDERS_DETAIL WHERE ID = '$id'

 

and then loop through them with a WHILE

and do something like this

$total = 0;
while($res = mysqli_fetch_array...)
{
   $total += $res[PRODPRICE ]*$res[PRODQTY];
}

 

then, after the while loop, you insert all this into ORDERS

Link to comment
https://forums.phpfreaks.com/topic/150287-solved-while-error/#findComment-789315
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.