嗨我想问一下如何才能获得物品的总成本。我有购物车,它只显示每个项目的价格数量和成本。我希望得到用户添加的所有项目的总费用请帮助
这是我的购物车代码
<?php
//Start the session
session_start();
//Create 'cart' if it doesn't already exist
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }
//Add an item only if we have the threee required pices of information: name, price, qty
if (isset($_GET['add']) && isset($_GET['price']) && isset($_GET['qty'])){
//Adding an Item
//Store it in a Array
$ITEM = array(
//Item name
'item_name' => $_GET['add'],
//Item Price
'price' => $_GET['price'],
//Qty wanted of item
'qty' => $_GET['qty']
);
//Add this item to the shopping cart
$_SESSION['SHOPPING_CART'][] = $ITEM;
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
//Allowing the modification of individual items no longer keeps this a simple shopping cart.
//We only support emptying and removing
else if (isset($_GET['remove'])){
//Remove the item from the cart
unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
//Re-organize the cart
//array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_GET['empty'])){
//Clear Cart by destroying all the data in the session
session_destroy();
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_POST['update'])) {
//Updates Qty for all items
foreach ($_POST['items_qty'] as $itemID => $qty) {
//If the Qty is "0" remove it from the cart
if ($qty == 0) {
//Remove it from the cart
unset($_SESSION['SHOPPING_CART'][$itemID]);
}
else if($qty >= 1) {
//Update to the new Qty
$_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
}
}
//Clear the POST variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $_SERVER['HTTP_HOST']; ?> Online Order Form</title>
<style>
#formArea #orderForm #formColumns {
width:650px;
margin:auto;
}
#formArea #orderForm #formColumns #leftColumn {
float:left;
width:300px;
}
#orderForm {
height:500px;
}
#formArea #orderForm #formColumns #rightColumn {
float:right;
width:300px;
}
#formArea #orderForm #formColumns th {
text-align: left;
}
.copyright {
font-size: 9pt;
}
</head>
<body>
<?php
//Print all the items in the shopping cart
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
?>
<tr id="item<?php echo $itemNumber; ?>">
<td><a href="?remove=<?php echo $itemNumber; ?>">remove</a></td>
<td><?php echo $item['item_name']; ?></td>
<td><?php echo $item['price']; ?></td>
<td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="2" maxlength="3" /></td>
<td><?php echo $item['qty'] * $item['price']; ?></td>
</tr>
<?php
}
?>
</table>
<?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
<p>
<label>
<input type="submit" name="update" id="update" value="Update Cart" />
</label>
</p>
</form>
<p><a href="javascript: history.go(-1)">Keep Shopping</a> - <a href="?empty">Empty</a> Cart</p>
</div>
<label>
<form action="form.php">
<input type="submit" id="checkout" value="Check Out" />
</form>
</body>
</html>
答案 0 :(得分:0)
在此片段中,您只需添加$total
变量:
<?php
//Print all the items in the shopping cart
//## Declare your total value
$total = 0;
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
//## Count item price value
$price_value = $item['qty'] * $item['price'];
//## Sum prices:
$total += $price_value;
?>
<tr id="item<?php echo $itemNumber; ?>">
<td><a href="?remove=<?php echo $itemNumber; ?>">remove</a></td>
<td><?php echo $item['item_name']; ?></td>
<td><?php echo $item['price']; ?></td>
<td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="2" maxlength="3" /></td>
<td><?php echo $price_value ?></td><tr>
<?php
}
?>
<tr>
<td colspan="4">Sum:</td>
<td> <?php echo $total; //## echo your total ?> </td>
</tr>