创建一个篮子的总数时出错

时间:2012-03-17 19:43:56

标签: php

我收到有关未定义变量的错误。但是,计算是正确的,它显示总计,但错误仍然显示在页面中 - 这是错误:

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>

1 个答案:

答案 0 :(得分:3)

上面似乎没有定义$grand_total变量。

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

此外,您可以将错误报告更改为不显示error_reporting()

的通知/警告