tags:

views:

551

answers:

9

I have a bunch a values I would like to add together which are entered into a form. Right now, the form has 11 lines but it could get larger in the future. I can easily add all the values together with something like:

$total = $value1 + $value2 + ... + $value11;

All the values I want to add together are coming from an HTML form. I want to avoid javascript.

But, I want to avoid having to manually do it, especially if it grows much larger. This is my attempt at adding all the values together using a loop but it returns an "undefined variable" error (it is just some test code to try out the idea):

<?php

$tempTotal = 0;

$pBalance1 = 5;
$pBalance2 = 5;
$pBalance3 = 5;

for  ($i = 1 ; $i <= 3 ; $i++){
    $tempTotal = $tempTotal + $pBalance.$i;
}


echo $tempTotal;

?>

Is what I want to do possible in PHP?

+2  A: 

Uhm why don't you use an array? If you give the forms a name like foobar[] it will be an array in PHP.

unexist
That what I am speaking of. When the name of every field in the form has the same name that ends with [] it will be an array in $_POST.
unexist
I never knew that! Thank you. That could come in very useful.
Haabda
+7  A: 
for  ($i = 1 ; $i <= 3 ; $i++){
    $varName = "pBalance".$i;
    $tempTotal += $$varName;
}

This will do what you want. However you might indeed consider using an array for this kind of thing.

_Lasar
That worked! Thank you for the quick solution.
Haabda
You can also use `$('variable_name'.$var}`
UltimateBrent
+1  A: 

The values you need are posted from a form, right? If so, you could iterate through the keys in the $_POST variable that match your form field's generated names.

foreach($_POST as $key=>$value)
{
  if(strpos($key, 'pBalance')===0)
  {
    $final_total += $value;
  }
}
Factor Mystic
+1  A: 

Try

$varName = 'pBalance' . $i;
$tempTotal = $tempTotal + $$varName;
Doug T.
+1  A: 

You can use an array to store your data, and just loop over it.

$tempTotal = 0;

$balances[] = 5;
$balances[] = 5;
$balances[] = 5;

for  ($i = 0; $i <= count($balances); $i++) {
    $tempTotal = $tempTotal + $balances[$i];
}

Or for brevity, use a foreach loop:

foreach($balances as $balance) {
    $tempTotal += $balance;
}
conmulligan
A: 

The concept you're looking for is called a variable variable (at least it's called that in PHP). Here is the official documentation and a useful tutorial. The syntax for a variable variable is a double dollar sign ($$).

VirtuosiMedia
+5  A: 

I would use @unexist's solution.. give the input fields names like:

<input name="myInput[]" />
<input name="myInput[]" />
<input name="myInput[]" />
...

Then in your PHP get the sum like this:

$total = array_sum($_REQUEST['myInput']);

Because of the '[]' at the end of each input name, PHP will make $_REQUEST['myInput'] automatically be an array. A handy feature of PHP!

Keeth
+1  A: 

If you're trying to collect the values of a POST, you should really use an array. You can avoid having to manually piece together such an array by using:

<input type="text" name="vals[]" value="one" />
<input type="text" name="vals[]" value="two" />

$_POST["vals"] will then be array("one", "two");

William Keller
A: 

In about 99% of all cases involving a PHP generated variable, you are doing The Wrong Thing. I'll just re-iterate what others have said:

USE AN ARRAY

Jonathan Arkell