1

わかりました、まさに私が期待していたものではありません...私は自分のコードがとても読みやすいとは知りませんでした...申し訳ありません! 修正するにはどうすればよいですか? 私は本当に(私が思っていた)合計を得るために単純な数学を達成したいと思っています。私はいたるところを見て、配列に関する非常に多くの情報を読みましたが、明らかに概念を理解していません...これ以上の助けを歓迎し、大いに感謝します!


ラジオ ボタン、チェックボックスがあり、配列を使用して合計購入金額を表示するモック注文フォームを作成しています。私が持っている2つの異なる配列から合計金額を取得できないことを除いて、機能しているフォームがあります。$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つエラーがあるのではないかと思います。理由はわかりませんが、他の誰かがこのコードを実行すると、計算は彼の$1.50の「追加」アイテムに対してのみ実行されることがわかります。実際の合計はありません。

于 2011-06-24T15:49:03.917 に答える