添加两个变量 - php [英] Adding two variables - php

查看:177
本文介绍了添加两个变量 - php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am in the process of designing a php validated form for a restaurant website which directs the user to a thank you page, displaying their reservation details, once they have pressed submit. Currently I have 2 variables, one named 'party', the other named 'vip'. These variables both display a string value when echoed. However, I wish to assign them an int value and then add them together to display a total price to the user.

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

Currently I have this code which does not seem to function:

<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;
?>

如果有人知道如何让这个工作我会非常感激,我是网络新手如果我的回答不是很明确,我会提前道歉。谢谢。

If anyone knows how I can get this to work I would be very grateful, I am new to web languages so I apologise in advance if my answer isn't very clear. Thank you.

推荐答案

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

Instead of using a string to determine the number of people why not just use an 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 应该只是否则,如果

这篇关于添加两个变量 - php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆