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
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
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
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
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
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
Share on other sites

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.