我的PHP文件不会计算用户输入并且不会输出段落?

时间:2016-01-23 23:53:31

标签: php html

为什么赢了&#t;" $ total_pizza_size = $ pizza_size; 和                  $ total_toppings = $ toppings * 1.25; "计算?我在这个程序的开头做了我的变量,它不断收到错误"致命错误:第50行&#34的/Applications/XAMPP/xamppfiles....php中不支持的操作数类型;

当我运行程序时,底部的段落也不会显示。发生了什么事?

*<!DOCTYPE html>
<html>
<head>
    <title>Your Pizza Order</title>
    <link href="main.css" rel="stylesheet" type="text/css">
</head>

<body>
        <h3>Your Pizza Order</h3><?php

    $pizza_size = array (
        "pizza_size"  => array("medium" => 7, "large" => 9, "family" => 9));

            //size
        $size = filter_input(INPUT_POST,'size');
            echo "You selected size $size @ <br>";

            //toppings
          $toppings = filter_input(INPUT_POST,'toppings',
                    FILTER_SANITIZE_SPECIAL_CHARS,FILTER_REQUIRE_ARRAY);

            if($toppings != NULL) {
                $num_toppings = count($toppings);
                echo "<br>You chose these $num_toppings toppings @ $1.25 each<br>";
                foreach($toppings as $key => $value) {
                    echo "$value<br>";
                }
            }
            //other items
            $other_items = filter_input (INPUT_POST, 'other_items',
                    FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);

            if ($other_items !== NULL) {
                $num_other_items = count($other_items);
                //echo $key. ' = ' . $value . '<br>';
                echo "<br>You also chose extras $num_other_items @ $1.50 each<br>";
                foreach ($other_items as $key2 => $value2) {
                }
            } 

            //request
            $request = filter_input(INPUT_POST,'request');
            $request = nl2br($request,false);
            if($request != '') {
                echo "<br>You made this request:<br>";
                echo $request;
            }

            $total_pizza_size = $pizza_size;
            $total_toppings = $toppings * 1.25;
            $total_other_items = $other_items * 1.50;
            $total = $total_pizza_size + $total_other_items + $total_toppings;
            $total_f ='$'.number_format($total, 2);
            ?>

    <p>Your total amount due on delivery is <?php echo $total_f;  ?></p>
    <p>Your pizza will be delivered
    to:</p><span><?php echo $address_one; ?></span><br>
    <h2>Thanks for ordering from the Pizza Place</h2>
    <p>We will call <span><?php echo $phone_number; ?></span><br></p>

*

4 个答案:

答案 0 :(得分:0)

您无法对整个数组求和。你必须遍历数组项......

foreach ($pizza_size as $key => $value) {
 .....
}

答案 1 :(得分:0)

似乎$ toppings是一个数组,它可能是你做错事的地方。 也许是array_sum($ toppings)* 1.25?

答案 2 :(得分:0)

$pizza_size定义为数组,而不是整数/实数。

您必须像这样更改代码:

$total_pizza_size = $pizza_size['pizza_size']['medium'];

(编辑:)

$total_pizza_size = $pizza_size['pizza_size'][$size];

如果我理解你的代码。

我不知道传递给$toppings

的值

答案 3 :(得分:0)

底部的段落未显示,因为您上面有语法错误。脚本退出。

尝试:

$ total_toppings = count($ toppings)* 1.25

$ total_pizza_size = $ size

你不能将标量值(1.25)X乘以数组($ toppings)。 $ total_pizza_size有同样的问题。您需要使用所选的大小而不是整个数组。