Schlo_50 Posted February 5, 2008 Share Posted February 5, 2008 Hi, I have prices stored in an array and printed on a page. They appear as: 'Price £2.00, £3.00' etc I want to be able to use some php maths to calculate the total of each value in the array. $orderid = array(); $quantity = array(); $price = array(); foreach($_POST['orderid'] as $id){ $orderid[] = $id; $quantity[] = $_POST['quantity'][$id]; $price[] = $_POST['price'][$id]; } $price = implode(',', $price); How can i integrate what i want into the code above? Thanks Link to comment https://forums.phpfreaks.com/topic/89549-solved-php-maths/ Share on other sites More sharing options...
aebstract Posted February 5, 2008 Share Posted February 5, 2008 $orderid = array(); $quantity = array(); $price = array(); $total = 0; foreach($_POST['orderid'] as $id){ $orderid[] = $id; $quantity[] = $_POST['quantity'][$id]; $price[] = $_POST['price'][$id]; $total = "$price[] + $total"; } $price = implode(',', $price); Try that and see if it does what you need. Link to comment https://forums.phpfreaks.com/topic/89549-solved-php-maths/#findComment-458700 Share on other sites More sharing options...
Schlo_50 Posted February 5, 2008 Author Share Posted February 5, 2008 I've just realised that the system will need to multiply the prices according to the quantities stated by the user.. More complications! Link to comment https://forums.phpfreaks.com/topic/89549-solved-php-maths/#findComment-458701 Share on other sites More sharing options...
Schlo_50 Posted February 5, 2008 Author Share Posted February 5, 2008 $orderid = array(); $quantity = array(); $price = array(); $total = 0; foreach($_POST['orderid'] as $id){ $orderid[] = $id; $quantity[] = $_POST['quantity'][$id]; $price[] = $_POST['price'][$id]; $total = "$price[] + $total"; } $price = implode(',', $price); Try that and see if it does what you need. Fatal error: Cannot use [] for reading in C:\apachefriends\xampp\htdocs\wdlayout\products.php on line 26 Link to comment https://forums.phpfreaks.com/topic/89549-solved-php-maths/#findComment-458705 Share on other sites More sharing options...
aebstract Posted February 5, 2008 Share Posted February 5, 2008 $orderid = array(); $quantity = array(); $price = array(); $total = 0; foreach($_POST['orderid'] as $id){ $orderid[] = $id; $quantity[] = $_POST['quantity'][$id]; $price[] = $_POST['price'][$id]; $total = "$price + $total"; } $price = implode(',', $price); You need to multiply the totals by eachother? Like 6.59 * 7.54 * 7.01 etc? If so, change that + sign between price and total to a * Link to comment https://forums.phpfreaks.com/topic/89549-solved-php-maths/#findComment-458711 Share on other sites More sharing options...
Schlo_50 Posted February 5, 2008 Author Share Posted February 5, 2008 My script is for order conformation, so it takes the values the user has selected and displays them for the user to review. I have three arrays. 1) Item ID 2) Quantity 3) Prices (They are all reletive so in the example below, item# 17 is 2.00 and the user would like 3 of them. The output is E.g Item # |#17|#12|#75 Quantity|3 |15 |9 Prices |2.00|1.00|3.00 I need the prices to be multiplied by the quantity the user wants and then the sum of them displayed. Link to comment https://forums.phpfreaks.com/topic/89549-solved-php-maths/#findComment-458722 Share on other sites More sharing options...
schilly Posted February 5, 2008 Share Posted February 5, 2008 $orderid = array(); $quantity = array(); $price = array(); foreach($_POST['orderid'] as $id){ $orderid[] = $id; $quantity[] = $_POST['quantity'][$id]; $price[] = $_POST['price'][$id]; $subtotals[] = $_POST['price'][$id] * $_POST['quantity'][$id]; $total += $_POST['price'][$id] * $_POST['quantity'][$id]; } $price = implode(',', $price); Something like that? Link to comment https://forums.phpfreaks.com/topic/89549-solved-php-maths/#findComment-458779 Share on other sites More sharing options...
Schlo_50 Posted February 5, 2008 Author Share Posted February 5, 2008 wow, exactly that. I was beginning to look at using for and while loops etc.. Thanks Link to comment https://forums.phpfreaks.com/topic/89549-solved-php-maths/#findComment-458787 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.