PHP计算不正确
我的页面上显示了一个总价,但是有些东西使这个显示不正确。我不确定是什么导致价格计算错误,但我想知道如何解决这个问题 这是我的密码:PHP计算不正确,php,mysql,html-table,Php,Mysql,Html Table,我的页面上显示了一个总价,但是有些东西使这个显示不正确。我不确定是什么导致价格计算错误,但我想知道如何解决这个问题 这是我的密码: function cart() { $total=0; $output = ''; $output .= '<center>'; $output .= '<table class="menufinal" border=0 width=75%>'; $output .= '<tr>'; $output .= '<th>
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;
}
功能购物车(){
$total=0;
$output='';
$output.='';
$output.='';
$output.='';
$output.='删除项目';
$output.='项目名称';
$output.='项目价格';
$输出='数量';
$output.='行总计';
$output.='';
foreach($\会话为$name=>$value){
如果($value>0){
如果(substr($name,0,5)='cart_u2;'){
$id=substr($name,5,(strlen($name)-5));
$get=mysql\u query('SELECT id,name,price FROM products WHERE id='.mysql\u real\u escape\u string((int)$id));
而($get\u row=mysql\u fetch\u assoc($get)){
$sub=$get_行['price']*$value;
$output.='';
$output.='
';
$output.=''.$get_行['name'].';
$output.='£'.number_格式($get_row['price'],2)。'';
$output.=''.$value'.';
$output.='£'.number_格式($sub,2)。'';
$output.='';
}
}
如果(空($总数)){
如果(空($sub)){
//无所事事
}否则{
$total=$sub;
}
}否则{
$total+=$sub;
}
$\会话['total']=$total;
}
}
$output.='';
如果(空($总数)){
会话_destroy();
标题('Location:index.php');
出口
}
$output.='
总计:£'.number_格式($Total,2)。'
';
$output.='
';
echo$输出;
}
添加应该在while
循环中完成,并且init$total
使用0。只需在while循环$sub=$get_row['price']*$value之后编写这些$合计+=$sub代码>不需要你的'if(empty($total))…这些if-else语句。你能说明一下你的数据源吗,以及循环前后的期望值。以及准确的实际输出,即您认为错误或意外的内容,这将有助于我们更好地理解您的问题