addition

If i have a field id $item1 and a field id $item2 how do i add them together and have the result as a hidden value $item3.
thanks Mick

to merge the value you want what’s called concatenation:

[php]
$item3 = $item1.$item2;
[/php]

To add them together is in adding numbers:

[php]
$item3 = $item1+$item2;
[/php]

I have tried this i have 5 different options each option has the 2 values added together to result in a third value. One of the 2 values is a fixed number. For example in option one if someone input 100 as a variable and i have a fixed value of 5 then the third value result is 105. if all the 5 options are filled out at 100 then each value results 105. but when i do a sum total it doesnt work out. If i fill all 5 options out i get a result of 525 which is correct but if i only fill out 3 options i get a result of 325 not 315 it seems to also add up the fixed value of 5 in the two other options i didnt fill out. I want the user to have a choice of filling up to 5 options out and depending on whether they fill one or all five it will total up to the relevant amount. each option has a variable amount and a fixed amount resulting in a third value. I been trying to do this now for 3 days. Any advice please.

its hard to say without seeing your code but it sounds like a series of if statements could work, only doing the maths when an input has been filled in.

I will forward my code thanks

$c1d1sum = $c1d1 + 5;
$c1d2sum = $c1d2 + 5;
$c1d3sum = $c1d3 + 5;
$c1d4sum = $c1d4 + 5;
$c1d5sum = $c1d5 + 5;

$sum_total = $c1d1sum + $c1d2sum + $c1d3sum + $c1d4sum + $c1d5sum + 100;

if i only fill out 3 fields still adds the value of 5 to fields 4 and 5

Using a tenery operator you can do checks inline

A tenery is an if else in one line.

[php]
//this was only for my tests, only $c1d1, $c1d2 and $c1d5 will be counted.
$c1d1 = 1;
$c1d2 = 1;
$c1d3 = ‘’;
$c1d4 = ‘’;
$c1d5 = 1;

$c1d1sum = is_numeric($c1d1) ? $c1d1 + 5 : 0;
$c1d2sum = is_numeric($c1d2) ? $c1d2 + 5 : 0;
$c1d3sum = is_numeric($c1d3) ? $c1d3 + 5 : 0;
$c1d4sum = is_numeric($c1d4) ? $c1d4 + 5 : 0;
$c1d5sum = is_numeric($c1d5) ? $c1d5 + 5 : 0;

$sum_total = $c1d1sum + $c1d2sum + $c1d3sum + $c1d4sum + $c1d5sum + 100;
echo $sum_total;
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service