在 PHP 中对数字数组值执行数学运算

发布于 2024-11-16 20:41:45 字数 3767 浏览 0 评论 0原文

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


我正在创建一个模拟订单表单,其中包含单选按钮、复选框并使用数组来显示总购买金额。我有一个正在工作的表格,除了我无法从我拥有的两个不同数组中获取总金额。 $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>

Ok, not exactly what I was expecting...I didn't know my code was so ureadable...sorry! What can I do to fix it? I really would like to just accomplish the (what I thought was) simple math to get totals. I've looked everywhere and read so much information on arrays and obviously I am just not grasping the concept...any more help is welcomed and would be GREATLY appreciated!


I’m creating a mock order form that has radio buttons, checkboxes and uses arrays to show the total purchase amount. I have a form that is working except for that I can't get the total amount from the two different arrays i have. $total = $extras + $additional isn't working and honestly, i should have known it couldn’t be that easy! ... Any suggestions on what formula to use so that I can get a total dollar amount of all of the options that are selected? Also, can anyone help me so that checkbox items are listed in a new row, and not a whole new table?

Thanks in advance!

A couple more things: I have to keep this in a redux and would like to keep the output in the table like it is...other than that, feel free to change whatever you want/need.

I’m new to PHP arrays and seem to only be having difficulties when it comes to their values, but since I know how important arrays are in PHP I would like to see how they work!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2024-11-23 20:41:45

评论指出了一些问题,主要的问题是代码的格式。事实上,当试图找出脚本中的错误时,混乱的格式可能会浪费数小时的时间。

您可能注意到的第一件事是您的 $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">
.....

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

The comments have pointed out some issues, the main one being the formatting of your code. Indeed, when trying to figure out what one did wrong in a script, confusing formatting can add hours of wasted time.

The first thing you might notice is that your $value array is missing a comma.

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

Formatting is to some degree a matter of style, but the main point is readability, so that you can more easily catch mistakes like this.

Here is a condensed version of something what your script might look like:

<?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">
.....

There may be further issues, as I'm not clear on which part you're having trouble, but they will now be much easier to debug.

暖树树初阳… 2024-11-23 20:41:45

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

Impressive code Doug. I hate to tell you, but I think there may be one more error. I can't figure out why, but if anyone else runs this code they would see that the math is only performed on his $1.50 "additional" items-- There isn't a working collective total.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文