douzhi9635 2017-02-25 18:02
浏览 48
已采纳

用php添加多个变量

So I am working on an assignment where I have to make a pizza order. Im new to PHP and am struggling to get my math to work. So this is my HTML

<!doctype html>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta http-equiv="expires" content="0" />
<html lang="en">
<head>
  <title> HW05 </title>
  <link rel="stylesheet" href="hw05.css">
</head>
<body>
<form method="post" action="hw05.php">
<header>
    <h1 id="title">
        Pizza Order
    </h1>
</header>
<section id="ci">
    <h2 id="ciheader">
        Customer Information
    </h2>
    <p id="text">
        Name: <input type="text" name="name" size="15" /> <br /><br />
        Phone: <input type="text" name="number" size="15" /> <br /> <br />
            <input type="submit" value="Order" />
    </p>
</section>

<section id="size">
    <h2 id="sizeheader">
        Pizze Size
    </h2>
    <p id="radio">
        <input type="radio" name="size1" value="small"> Small <br />
        <input type="radio" name="size2" value="medium"> Medium <br />
        <input type="radio" name="size3" value="large"> Large
    </p>
</section>

<section id="top">
    <h2 id="topheader">
        Toppings
    </h2>
    <p id="checkbox">
        <input type="checkbox" name="checkbox1" value="box"> Pepperoni <br />
        <input type="checkbox" name="checkbox2" value="box"> Sausage <br />
        <input type="checkbox" name="checkbox3" value="box"> Mushroom <br />
        <input type="checkbox" name="checkbox4" value="box"> Ham <br />
        <input type="checkbox" name="checkbox5" value="box"> Olives <br />
        <input type="checkbox" name="checkbox6" value="box"> Onions
    </p> 
</section>

<section id="price">
    <h2 id="priceheader">
        Topping Price
    </h2>
    <p id="dollars">
        $1.00 <br />
        $1.50 <br />
        $0.80 <br />
        $1.25 <br />
        $0.75 <br />
        $0.50
    </p>
</section>
</form>
</body>
</html>

So from the HTML I need to create a PHP page that counts the number of toppings and adds the total cost of the size and the toppings together. I can not figure out how to make the PHP add the cost of each topping and the cost of the pizza size together. I was able to get the PHP to count the toppings though. Any help would be great thanks.

  • 写回答

4条回答 默认 最新

  • doumou3883 2017-02-25 18:19
    关注

    You could do something like:

    <input type="checkbox" name="topping[]" value="pepperoni"> Pepperoni <br />
    <input type="checkbox" name="topping[]" value="sausage"> Sausage <br />
    <input type="checkbox" name="topping[]" value="mushroom"> Mushroom <br />
    <input type="checkbox" name="topping[]" value="ham"> Ham <br />
    <input type="checkbox" name="topping[]" value="olives"> Olives <br />
    <input type="checkbox" name="topping[]" value="onions"> Onions
    

    In your hw05.php file:

    $toppingToPriceMap = [
        "pepperoni" => 1,
        "sausage" => 0.8,
        "mushroom" => 1.25,
        "ham" => 0.75,
        "olives" => 0.5,
        "onions" => 0.75
    ];
    if (isset($_POST["topping"]) {
       $numberOfToppings = count($_POST["topping"]);
       $cost = 0;
       foreach ($_POST["topping"] as $topping) {
           if (isset($toppingToPriceMap[$topping])) {
              $cost += $toppingToPriceMap[$topping]; 
           }
       }       
    }
    
    echo "Selected $numberOfToppings toppings at a cost of $".$cost;
    

    Note: This is just one of the fixes that your code requires.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?