私が期待していたものではありません...私のコードは非常に壊れやすいものではありませんでした...ごめんなさい!それを修正するために私は何ができますか?私は本当に(私が思ったように)合計を得るための簡単な数学を達成したいと思います。私はどこでも見て、配列に関する情報をたくさん読んでいます。明らかに私はコンセプトを理解していません...それ以上の助けが歓迎され、大いに感謝されるでしょう!数値配列の数値をPHPで計算する
私はラジオボタン、チェックボックスがあり、合計購入金額を表示するために配列を使用モック注文フォームを作成しています。私は私が持っている2つの異なる配列から合計量を得ることができないことを除いて動作しているフォームがあります。 $ total = $ extras + $ additionalは動作しておらず、正直なところ、私はそれが簡単ではないことを知っていたはずです! ...選択されているすべてのオプションの合計金額を得ることができるように、どのような式を使用するかに関する提案はありますか?また、チェックボックスの項目が新しい行ではなく、まったく新しいテーブルに表示されるように誰も助けてくれますか?
ありがとうございます!
カップル以上のこと:私はこれを還元しておかなければならず、テーブルの出力をそのままにしておきたいと思っています。それ以外のものは自由に変更してください。
私は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>
あなたはhtmlタグの前にテーブルをエコーしています。それを行う良い方法ではありません – Ibu
このコードは読めません。 – kapa
インデントはあなたの友人です – dqhendricks