我試圖抓住我的購物車的總計,但我似乎無法讓它工作,我想知道如果它的方式我已經接近我的代碼,我有使用foreach循環從會話數組中獲取id,然後循環遍歷結果,但是當我嘗試執行SUM查詢時,答案僅輸出單個價格作爲數組,以便將它們添加到一起。將價格存儲在二維數組中會更好嗎?不能得到一個購物車的總計PHP/MySQL
if(!isset($_SESSION['cart'])){//
$_SESSION['cart']=array();//creating session array to store items id
}
<?php
echo'<table class="table">';
echo'<tr>';
echo'<th>Item</th>';
echo'<th>Code</th>';
echo'<th>Description</th>';
echo'<th>Cost(GBP)</th>';
echo'<th>Remove</th>';
echo'</tr>';
foreach ($_SESSION['cart'] as $value){
$z = mysql_query('SELECT * FROM product_item where id="'.$value.'"');
while($row = mysql_fetch_array($z)){
echo'<tr>';
echo'<td><img src="images/'.$row['path'].'.jpg" alt="'.$row['alt'].'" width="65" height="65"/></td>';
echo'<td>'.$row['code'].'</td>';
echo'<td>'.$row['description'].'</td>';
echo'<td><p>£'.$row['price'].'</p></td>';
echo'<td><p><a title="remove from shopping cart" href="cart.php?remove='.$value.'">X</a></p></td>';
echo'</tr>';
}
// this is where i want to get total cost
$y = mysql_query('SELECT SUM(price) as total_cost FROM product_item where id="'.$value.'"');
while($row = mysql_fetch_array($y)){
echo $row['total_cost'];
}
}
echo'<tr>';
echo'<td></td>';
echo'<td></td>';
echo'<td></td>';
echo'<td><p>Total</p></td>';
echo'<td></td>';
echo'</tr>';
echo'</table>';
}
?>
?>
你嘗試直接在SQL查詢?它可能是查詢 – Joseph 2012-02-16 08:29:44