简体   繁体   中英

How would I add a PHP statement to conditionally subtract?

I have a form that uses PHP to calculate a total based on the selections that the user makes. There are 13 selections total. There are two sections that I am stuck on, "Blue" and "Orange":

If the User selects "Blue", $250.00 is added to the price

If the User selects "Orange", $150.00 is added to the price

The thing I am trying to figure out: I would like to make it so that if the user selects Blue, and then goes on to select Orange - the charge for Orange would not be added to the total. On the other hand, if they did not select Blue, and went on to select Orange, I would want the $150.00 to get added to the total.

The only way that I can think of to explain it is that if you buy Blue, Orange is included.

Here is my HTML:

BLUE

Yes No

ORANGE

Yes

No

Here is my PHP:

 <?php $one = $_POST ['one']; $two = $_POST ['two']; $three = $_POST ['three']; $four = $_POST ['four']; $five = $_POST ['five']; $six = $_POST ['six']; $seven = $_POST ['seven']; $eight = $_POST ['eight']; $nine = $_POST ['nine']; $ten = $_POST ['ten']; $eleven = $_POST ['eleven']; $twelve = $_POST ['twelve']; $thirteen = $_POST ['thirteen']; $total = ($_POST['one'] + $_POST['two'] + $_POST['three'] + $_POST['four'] + $_POST['five'] + $_POST['six'] + $_POST['seven'] + $_POST['eight']+ $_POST['nine'] + $_POST['ten']+ $_POST['eleven'] + $_POST['twelve']+ $_POST['thirteen']); 

Someone told me that I should make it so that:

 $four = $_POST ['four']- $_POST ['six']; 

and that works fine, except that even if Blue (four) is not selected, and Orange (six) is, the value for Orange will not get added to the total. Should I be using an if/else statement or something? Also, this form does not need to be secure. Thanks in advance for any ideas.

If Orange is included when you get Blue, then it probably shouldn't be 2 different yes/no choices.

Instead, it should more likely be one question:


Which plan would you like? Orange (150) Blue (250)

Yes, an if/else is the way to go. You can accomplish this in-line with the ternary operator .

Change this:

$total = ($_POST['one'] + $_POST['two'] + $_POST['three'] + $_POST['four'] 
+   $_POST['five'] + $_POST['six'] + $_POST['seven'] + $_POST['eight']+ 
$_POST['nine'] + $_POST['ten']+ $_POST['eleven'] + 
$_POST['twelve']+ $_POST['thirteen']);

To this:

$total = ($_POST['one'] + $_POST['two'] + $_POST['three'] + $_POST['four'] 
+   $_POST['five'] + (($_POST['four'] > 0) ? 0 : $_POST['six']) + $_POST['seven'] + $_POST['eight']+ 
$_POST['nine'] + $_POST['ten']+ $_POST['eleven'] + 
$_POST['twelve']+ $_POST['thirteen']);

This assumes that your $_POST values are integers.

Yes you need some if/ else logic

if( empty($_POST['blue'] ) && !empty($_POST['orange'] ) )
{
    $_POST['total'] += $_POST['orange'];
}

Orange only gets added if blue if false. (note I use empty() to avoid warnings about undefined variables)

Out of curiosity why are you naming the post variables by number? Naming by name might make it less confusing when you have to modify it in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM