我的页面上显示总价格但是有些东西会导致显示不正确。我不确定是什么导致价格计算错误,但我想知道如何解决这个问题。
这是我的代码:
function cart() {
$total=0;
$output = '';
$output .= '<center>';
$output .= '<table class="menufinal" border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Remove Item</th>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
$output .= '<tr>';
$output .= '<td><a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br></td>';
$output .= '<td>' . $get_row['name'] . '</td>';
$output .= '<td>£ ' . number_format($get_row['price'], 2) . '</td>';
$output .= '<td><a href="cart.php?remove=' .$id. '"style="text-decoration:none"><strong>- </strong></a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a></td>';
$output .= '<td>£ ' . number_format($sub, 2) . '</td>';
$output .= '</tr>';
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
$_SESSION['total'] = $total;
}
}
$output .= '</table>';
if (empty($total)){
session_destroy();
header ('Location: index.php');
exit;
}
$output .= '<br><br><br><br><div id="finalPrice">Total: £ ' . number_format($total, 2) . '<br></div>';
$output .= '<br><br><br><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="110"> <a href="checkout.php"><img src="checkout.png" width="240" height="151"></a>';
echo $output;
}