1

好吧,不完全是我所期待的......我不知道我的代码是如此难以阅读......对不起!我能做些什么来修复它?我真的很想完成(我认为是)简单的数学运算来获得总数。我到处看了看,阅读了很多关于数组的信息,显然我只是没有理解这个概念......欢迎提供更多帮助,我们将不胜感激!


我正在创建一个模拟订单表单,其中包含单选按钮、复选框并使用数组来显示总购买金额。我有一个正在工作的表格,除了我无法从我拥有的两个不同数组中获得总量。$total = $extras + $additional 不起作用,老实说,我应该知道它不可能那么容易!...关于使用什么公式的任何建议,以便我可以获得所有选择的选项的总金额?另外,谁能帮我把复选框项目列在一个新行中,而不是一个全新的表中?

提前致谢!

还有几件事:我必须将其保留在 redux 中,并希望将输出保留在表中......除此之外,随意更改您想要/需要的任何内容。

我是 PHP 数组的新手,似乎只有在涉及到它们的值时才会遇到困难,但是因为我知道数组在 PHP 中的重要性,所以我想看看它们是如何工作的!

    <?php

    /*This stuff is only here because I want to make sure
    there are 2 decimal places in the final numbers since
    I'm dealing in "money" values*/
    $total = number_format ($total,2);
    $value = number_format ($value,2);
    $additional = number_format ($additional,2);


    $value = array("Short Trip"=>15.99, "Long Trip"=>28.99, "Overnight"=>10.99 "Forever"=>99.99);

    if(isset($_POST['travel'])) {

    $extras = array("Hair Brush"=>1.50, "Shampoo"=>1.50, "Toothpaste"=>1.50, 
    "Cream Rinse"=>1.50, "Tooth Brush"=>1.50, 
    "Shower Cap"=>1.50, "Washcloth"=>1.50, "Mouthwash"=>1.50);

    if (isset($_POST['extras'])) {
    foreach ($_POST['extras'] as $additional) {
    echo "<table border =\"2\">
    <tr><td>Item</td><td>Charges</td></tr>
    <tr><td>".$_POST['travel']."</td>
    <td> $".$value[$_POST['travel']]."</td></tr>
    <tr>

    <td>".$additional."</td>
    <td> $".$extras[$additional]."</td>

    </tr>
    <tr><td>Your total</td> <td>".$total."</td></tr>
    </table>";

    }
    }
    }

    ?>


    <html>
    <body>
    <form action="" method="post">

    <table border="2">
    <tr>
    <td colspan="2" align="center" scope="col">Stay Information</td>
    </tr>
    <tr>
    <td><input type="radio" name="travel" value="Short Trip" />Short trip $15.99</td>
    <td><input type="radio" name="travel" value="Long Trip" />Long trip $28.99</td>
    </tr>
    <tr>
    <td><input type="radio" name="travel" value="Overnight" />Overnight $10.99</td>
    <td><input type="radio" name="travel" value="Forever" />Forever $99.99</td>
    </tr>
    </table>
    <table border="2">
    <tr>
    <td colspan="2" scope="col">What will you need?($1.50 each)</td>
    </tr>
    <tr>
    <td><input type="checkbox" name="extras[]" value="Hair Brush" />Hair Brush</td>
    <td><input type="checkbox" name="extras[]" value="Shampoo" />Shampoo</td></tr>
    <tr>
    <tr><td><input type="checkbox" name="extras[]" value="Toothpaste" />Toothpaste</td>
    <td><input type="checkbox" name="extras[]" value="Cream Rinse" />Cream Rinse</td></tr>
    </tr>
    <tr>
    <td><input type="checkbox" name="extras[]" value="Tooth Brush" />Tooth Brush</td>
    <td><input type="checkbox" name="extras[]" value="Shower Cap" />Shower Cap</td></tr>
    <tr>
    <tr><td><input type="checkbox" name="extras[]" value="Washcloth" />Washcloth</td>
    <td><input type="checkbox" name="extras[]" value="Mouthwash" />Mouthwash</td></tr>
    </tr>

    <tr><td colspan="2">
    <input type="submit" value="Submit"></td></tr>
    </table>
    </form>
    </body>
    </html>
4

2 回答 2

2

评论指出了一些问题,主要问题是代码的格式。事实上,当试图找出一个人在脚本中做错了什么时,混乱的格式会增加数小时的浪费时间。

您可能会注意到的第一件事是您的$value数组缺少逗号。

$value = array("Short Trip"=>15.99, "Long Trip"=>28.99, "Overnight"=>10.99 "Forever"=>99.99)
//  comma here -----------------------------------------------------------^

格式化在某种程度上是一种风格问题,但重点是可读性,这样你就可以更容易地发现这样的错误。

这是您的脚本可能看起来的内容的精简版本:

<?php

$value = array(
    "Short Trip" => 15.99, 
    "Long Trip" => 28.99, 
    "Overnight" => 10.99,
    "Forever" => 99.99
);

$extras = array(
    "Hair Brush" => 1.50, 
    "Shampoo" => 1.50, 
    "Toothpaste" => 1.50, 
    "Cream Rinse" => 1.50, 
    "Tooth Brush" => 1.50, 
    "Shower Cap" => 1.50, 
    "Washcloth" => 1.50, 
    "Mouthwash" => 1.50
);

// combine condititions
if (isset($_POST['travel']) && isset($_POST['extras'])) {

    $total = $value[$_POST['travel']];
    // start table html (before foreach loop)

    // store html in a variable to print later
    $html = "<table border =\"2\">
              <tr>
               <td>Item</td>
               <td>Charges</td>
              </tr>
              <tr>
                <td>" . $_POST['travel'] . "</td>
                <td> $" . $total . "</td>
              </tr>";

    foreach ($_POST['extras'] as $additional) {
        // add a row per extra
        $html .= "<tr>
                    <td>" . $additional . "</td>
                    <td> $" . $extras[$additional] . "</td>
                  </tr>";

        // increment total
        $total += $extras[$additional];
    }
    $html .= "<tr>
               <td>Your total</td> 
               <td>" . $total . "</td>
              </tr>
            </table>";

}
?>
<html>
<body>
<form action="" method="post">

<?php 
if (isset($html)) {
    echo $html;
}
?>

<table border="2">
.....

可能还有其他问题,因为我不清楚您在哪一部分遇到了问题,但现在它们会更容易调试。

于 2011-06-24T06:55:52.750 回答
0

令人印象深刻的代码道格。我不想告诉你,但我认为可能还有一个错误。我不知道为什么,但是如果其他人运行此代码,他们会发现仅在他的 1.50 美元“附加”项目上执行了数学运算——没有工作的集体总数。

于 2011-06-24T15:49:03.917 回答