我需要帮助获得一个简单的php购物车使用会话总数
大家好,我有一个购物车,它是由2个PHP文件。order.php和products.php 所有的配置都在order文件中设置,它不使用MYSQL数据库作为im使用会话 我试图得到所有项目的总和,是添加到购物车,但这是什么我挣扎。我可以得到添加到购物车中的每个项目的总数,但不能得到所有项目的总数。在此方面的任何帮助都将不胜感激 谢谢 PHP我需要帮助获得一个简单的php购物车使用会话总数,php,html,session,shopping-cart,cart,Php,Html,Session,Shopping Cart,Cart,大家好,我有一个购物车,它是由2个PHP文件。order.php和products.php 所有的配置都在order文件中设置,它不使用MYSQL数据库作为im使用会话 我试图得到所有项目的总和,是添加到购物车,但这是什么我挣扎。我可以得到添加到购物车中的每个项目的总数,但不能得到所有项目的总数。在此方面的任何帮助都将不胜感激 谢谢 PHP <?php //Start the session session_start(); //Create 'cart' if it doesn't
<?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
'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']);
}
?>
只要用下面我的代码替换这段代码就行了……应该可以了
<?php
//Print all the items in the shopping cart
$totalAll = 0;
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
$totalAll = $totalAll + ($item['qty']*$item['price']);
?>
<tr id="item<?php echo $itemNumber; ?>">
<td height="41"><a href="?remove=<?php echo $itemNumber; ?>">Remove Item</a></td>
<td><?php echo $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>
<td>£<?php echo $totalAll;?></td>
</tr>
<?php
}
?>
£
£
下面有您的答案。。。
<?php
//Print all the items in the shopping cart
$totalAll = 0;
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
$totalAll = $totalAll + ($item['qty']*$item['price']);
?>
<tr id="item<?php echo $itemNumber; ?>">
<td height="41"><a href="?remove=<?php echo $itemNumber; ?>">Remove Item</a></td>
<td><?php echo $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>
<td>£<?php echo $totalAll;?></td>
</tr>
<?php
}
?>