无法弄清楚为什么这不起作用?

时间:2011-08-25 13:00:14

标签: php

<?php

$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];

$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;

i    f ($totalqty == 0) {
    echo "You did not enter anything in the boxes on the previous page.";
}
else {
    echo "<p>Order processed at ".date('H:i, jS F Y')."</p><br /><br />";
    echo '<p>Your order is as follows:</p>';
    echo $tireqty.' tires<br />';
    echo $oilqty.' bottles of oil<br />';
    echo $sparkqty.' spark plugs<br />';
    echo "Items ordered: ".$totalqty."<br />";

    if ($tireqty < 10) {
        $discount = 0;
    }
    elseif ($tireqty >= 10) && ($tireqty <= 49) {
        $discount = 0.05;
    }
    elseif ($tireqty >= 50) && ($tireqty <= 99) {
        $discount = 0.10;
    }
    elseif ($tireqty >= 100) {
        $discount = 0.15;
    }

    $totalamount = ($tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE) * (1+$discount);

    echo "Subtotal (Discount applied here): $".number_format($totalamount, 2)."<br />";

    $taxrate = 0.10;
    $totalamount = $totalamount * (1+ $taxrate);
    echo "Total including Tax: $".number_format($totalamount,2)."<br />";
}
?>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:7)

我是初学者,所以我可能会错,但在这里,如果($ tireqty&gt; = 10)&amp;&amp; ($ tireqty&lt; = 49)我会使用和额外的括号:

elseif (($tireqty >= 10) && ($tireqty <= 49)) {
...
}

希望是这样:)

答案 1 :(得分:2)

这是一个可怕的问题但是这里......不知道什么是“不工作”我只能猜测:

i    f ($totalqty == 0) {

这是一个语法错误。你可能意味着:

if ($totalqty == 0) {

同样,这里:

if ($tireqty < 10) {
        $discount = 0;
    }
    elseif ($tireqty >= 10) && ($tireqty <= 49) {
        $discount = 0.05;
    }
    elseif ($tireqty >= 50) && ($tireqty <= 99) {
        $discount = 0.10;
    }
    elseif ($tireqty >= 100) {
        $discount = 0.15;
    }

整个条件需要用parens括起来:

elseif (($tireqty >= 10) && ($tireqty <= 49)) {
    $discount = 0.05;
}
elseif (($tireqty >= 50) && ($tireqty <= 99)) {
    $discount = 0.10;
}
elseif ($tireqty >= 100) {
    $discount = 0.15;
}

这里可能存在更多错误。请编辑您的问题并描述具体哪些无效,以及您采取了哪些措施来解决问题,以及您需要哪些帮助。