好吧,不完全是我所期待的......我不知道我的代码是如此难以理解......对不起!我该怎么办才能修复它?我真的很想完成(我认为的)简单的数学计算得到总数。我到处寻找并阅读有关阵列的大量信息,显然我只是没有理解这个概念......欢迎任何更多的帮助,我们将非常感激!
我正在创建一个模拟订单表单,其中包含单选按钮,复选框和使用数组来显示总购买金额。我有一个正在工作的表单,除了我无法从我拥有的两个不同数组中获得总量。 $ 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>
答案 0 :(得分: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">
.....
可能还有其他问题,因为我不清楚你遇到麻烦的部分,但现在调试起来会更容易。
答案 1 :(得分:0)
令人印象深刻的代码道格。我不想告诉你,但我认为可能还有一个错误。我无法弄清楚为什么,但如果其他人运行此代码,他们会看到数学仅在他的1.50美元“额外”项目上执行 - 没有工作集体总数。