2012-03-17 40 views
1

我收到一个关于未定义变量的错误。然而,计算是正确的,它显示的总计,但错误仍然在页面中出现 - 在这里是错误:创建一个篮子总计时出错

Notice: Undefined variable: grand_total in 

这里是我的代码:

$line_cost = $product['price'] * $item['quantity']; 
    $grand_total += $line_cost; 

    ?> 

    <tr> 
    <td><?=$product['common_name'];?></td> 
    <td><input type='text' name='quantity[]' value='<?=$item['quantity'];?>' size='2' /></td> 
    <td>&pound;<?=number_format($line_cost, 2);?></td> 
    </tr> 
    <!--- 
    Notice the [] on the field name for quantity - this means it is an array 
    The index of the array will start at 0 (i,e. the first product) and the second will be 1, etc... 
    ---> 
    <?php 

} 
?> 
    <!-- Final row to put a button to recalculate --> 
    <tr> 
    <td colspan='3' align='center'> 
     <input type='submit' name='recalc' value='Recalculate' /> 
    </td> 
    </tr> 
    <tr> 
    <td>&pound;<?=number_format($grand_total, 2);?></td> 
    </tr> 
</table> 

回答

3

看来$grand_total变量没有在上面定义。

$grand_total = isset($grand_total) ? $grand_total : 0; 
$line_cost = $product['price'] * $item['quantity']; 
$grand_total += $line_cost; 

您也可以更改错误报告不显示通知/带error_reporting()

+0

感谢警告,但因为现在我已经做了那怪异的,错误已经消失了,但它是不是显示的最后一行成本总计 – user1227124 2012-03-17 19:47:26

+0

辉煌!感谢matey你最好的 – user1227124 2012-03-17 19:50:37