Jump to content

[SOLVED] PHP maths


Schlo_50

Recommended Posts

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

$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

$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

$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

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

 

$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

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.