添加两个变量 - php

时间:2014-11-20 22:29:16

标签: php variables int add

我正在为餐馆网站设计一个经过php验证的表格,该网站将用户引导到感谢页面,一旦他们按下提交,就会显示他们的预订详情。目前我有2个变量,一个名为'party',另一个名为'vip'。这些变量在回显时都显示字符串值。但是,我希望为它们分配一个int值,然后将它们一起添加到用户显示总价格。

目前我有这个似乎不起作用的代码:

<b>Total Reservation Costs: </b> £
 <?php
 if ( $party == "1 Person (+£5)" )
   {
        $party = 5;
   }
    elseif if ( $party == "2 People (+£10)" )
   {
         $party = 10;
   }
   elseif if ( $party == "3 People (+£15)" )
   {
         $party = 15;
   }
   elseif if ( $party == "4 People (+£20)" )
   {
         $party = 20;
   }
    elseif if ( $party == "5 People (+£25)" )
   {
        $party = 25;
   }
   elseif if ( $party == "6 People (+£30)" )
   {
        $party = 30;
   }
    elseif if ( $party == "7 People (+£35)" )
   {
        $party = 35;
   }
   elseif if ( $party == "8 People (+£40)" )
   {
        $party = 40;
   }
   elseif if ( $party == "9 People (+£45)" )
   {
        $party = 45;
   }
   elseif if ( $party == "10+ People (+£50)" )
   {
        $party = 50;
   }

 if ( $vip == "Yes" )
   {
        $vip = 5;
   }
    elseif if ( $vip == "No" )
   {
        $vip = 0;
   }

 echo $party + $vip;
?>

如果有人知道如何让这个工作,我将非常感激,我是网络语言的新手,所以如果我的答案不是很清楚,我会提前道歉。谢谢。

2 个答案:

答案 0 :(得分:2)

而不是使用字符串来确定人数,为什么不使用int

这是一个完整的程序

<form action="" method="POST">
    <select name="party">
        <option value="1">1 People (+£5)</option> 
        <option value="2">2 People (+£10)</option>
        <!-- ... -->
        <option value="10">10+ People (+£50)</option>
    </select>
    <input type="checkbox" name="vip" />
    <input type="submit" value="Confirm Reservation" /> 
</form>

<?php
if (isset($_POST['party']) && is_numeric($_POST['party'])) {
    $party = (int)$_POST['party'];
    $vip = isset($_POST['vip']) ? 5 : 0;
    echo "Total is: " . (($party * 5) + $vip);
}

elseif if应该只是else if

答案 1 :(得分:0)

如果是,那么,如果它是其他的

Total Reservation Costs:  £
<?php
    if ( $party == "1 Person (+£5)" ){
    $party = 5;
    }
    elseif( $party == "2 People (+£10)" ){
    $party = 10;
    }
    elseif( $party == "3 People (+£15)" ){
    $party = 15;
    }
    elseif( $party == "4 People (+£20)" ){
    $party = 20;
    }
    elseif( $party == "5 People (+£25)" ){
    $party = 25;
    }
    elseif ( $party == "6 People (+£30)" ){
    $party = 30;
    }
    elseif( $party == "7 People (+£35)" ){
    $party = 35;
    }
    elseif( $party == "8 People (+£40)" ){
    $party = 40;
    }
    elseif( $party == "9 People (+£45)" ){
    $party = 45;
    }
    elseif( $party == "10+ People (+£50)" ){
    $party = 50;
    }
    else{    $party = 0;
    }
    if ( $vip == "Yes" ){
    $vip = 5;
    }
    elseif ( $vip == "No" ){
    $vip = 0;
    }
    else{
    $vip = 0;
    }
    echo $party + $vip;
?>
`

我建议添加一个else语句,正如我在答案中添加的那样,以处理可能发生的任何其他情况。