Reputation: 1433
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?
Upvotes: 1
Views: 1415
Reputation: 10804
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
Upvotes: 1
Reputation: 5410
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");
Upvotes: 2
Reputation: 2100
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!
Upvotes: 6
Reputation: 53356
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 ($$).
Upvotes: -1
Reputation: 7148
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;
}
Upvotes: 2
Reputation: 65599
Try
$varName = 'pBalance' . $i;
$tempTotal = $tempTotal + $$varName;
Upvotes: 0
Reputation: 26760
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;
}
}
Upvotes: 2
Reputation: 5437
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.
Upvotes: 7
Reputation: 2528
Uhm why don't you use an array? If you give the forms a name like foobar[] it will be an array in PHP.
Upvotes: 3