我需要使用会话获取总计一个简单的PHP购物车的帮助 [英] I need help getting the grand total of a simple php cart using sessions
问题描述
大家好我有一个由2个PHP文件组成的购物车。 order.php和products.php。
所有配置都在订单文件中设置,它不使用MYSQL数据库作为即时消息使用会话。
我试图获得所有添加到购物车的物品的总数,但这就是我正在努力的。我可以得到添加到购物车中的每件商品的总数,但不是所有商品的总数。任何帮助,这将不胜感激。
Thankyou
PHP
<?php
//开始会话
session_start();
//创建'cart'如果它还不存在
if(!isset($ _ SESSION ['SHOPPING_CART'])){$ _SESSION ['SHOPPING_CART'] = array( ); }
//仅当我们有三个必需的信息时才添加一个项目:名称,价格,数量
if(isset($ _ GET ['add']) && isset($ _ GET ['price'])&& isset($ _ GET ['qty'])){
//添加一个项目
//将它存储在一个Array
$ ITEM = array(
//商品名称
'name'=> $ _GET ['add'],
//商品价格
'price' => $ _GET ['price'],
//需要物品的数量
'qty'=> $ _GET ['qty']
);
//将此商品添加到购物车
$ _SESSION ['SHOPPING_CART'] [] = $ ITEM;
//清除URL变量
header('Location:'。$ _SERVER ['PHP_SELF']);
}
//允许修改个别项目不再保持这个简单的购物车。
//我们只支持清空和删除
else if(isset($ _ GET ['remove'])){
//从购物车中删除商品
unset($ _SESSION [ 'SHOPPING_CART'] [$ _ GET [ '删除']]);
//重新组织购物车
// array_unshift($ _SESSION ['SHOPPING_CART'],array_shift($ _SESSION ['SHOPPING_CART']));
//清除URL变量
header('Location:'。$ _SERVER ['PHP_SELF']);
$ b $ else if(isset($ _ GET ['empty'])){
//通过销毁会话中的所有数据清除Cart
session_destroy( );
//清除URL变量
header('Location:'。$ _SERVER ['PHP_SELF']);
$ b $ else if(isset($ _ POST ['update'])){
//更新所有项目的数量
foreach($ _POST ['items_qty '] as $ itemID => $ qty){
//如果Qty是0,请将它从购物车中移除
if($ qty == 0){
// Remove它从购物车
unset($ _ SESSION ['SHOPPING_CART'] [$ itemID]);
其他如果($ qty> = 1){
//更新新的数量
$ _SESSION ['SHOPPING_CART'] [$ itemID] ['qty' ] = $ qty;
}
}
//清除POST变量
header('Location:'。$ _SERVER ['PHP_SELF']);
}
?>
HTML FORM
< form action =method =postname =shoppingcart>
<?php
ob_start();
?>
< table width =700border =0align =centercellpadding =0cellspacing =0>
< tr>
< th width =143height =46scope =col>& nbsp;< / th>
< th width =177scope =col>项目名称< / th>
< th width =155scope =col>单价< / th>
< th width =130scope =col> Quantity< / th>
< th width =95scope =col> Total< / th>
< / tr>
<?php
//打印购物车中的所有商品
foreach($ _SESSION ['SHOPPING_CART'] $ itemNumber => $ item){
?>
< tr id =item<?php echo $ itemNumber;?>>
< td height =41>< a href =?remove =<?php echo $ itemNumber;?>>删除商品< / a>< / td>
< td><?php echo $ item ['name']; ?>< / TD>
< td>£<?php echo $ item ['price']; ?>< / TD>
< td>< input name =items_qty [<?php echo $ itemNumber;?>]type =textid =item<?php echo $ itemNumber;?> _qty value =<?php echo $ item ['qty'];?> size =2maxlength =3/>< / td>
< td>£<?php echo $ item ['qty'] * $ item ['price']; ?>< / TD>
< td>£<?php echo $ item ['total'] * $ item ['price']; ?>< / TD>
< / tr>
<?php
}
?>
< / table>
<?php $ _SESSION ['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
< p align =left>
< label>
< p>< a href =../ products.php>继续购物< / a> - < a href =?empty>清空购物车< / a>< br />
< br />
< input type =submitname =updateid =updatevalue =Update Cart/>
< br />
< br />
< / label>
< / form>
只需用下面的代码替换这段代码....应该工作...
<?php
//打印购物中的所有商品购物车
$ totalAll = 0;
foreach($ _SESSION ['SHOPPING_CART'] $ itemNumber => $ item){
$ totalAll = $ totalAll +($ item ['qty'] * $ item ['价钱']);
?>
< tr id =item<?php echo $ itemNumber;?>>
< td height =41>< a href =?remove =<?php echo $ itemNumber;?>>删除商品< / a>< / td>
< td><?php echo $ item ['name']; ?>< / TD>
< td>£<?php echo $ item ['price']; ?>< / TD>
< td>< input name =items_qty [<?php echo $ itemNumber;?>]type =textid =item<?php echo $ itemNumber;?> _qty value =<?php echo $ item ['qty'];?> size =2maxlength =3/>< / td>
< td>£<?php echo $ item ['qty'] * $ item ['price']; ?>< / TD>
< td>£<?php echo $ totalAll;?>< / td>
< / tr>
<?php
}
?>
Hi all I have a shopping cart which is made up of 2 PHP files. order.php and products.php.
All of the configuration is set in the order file and it doesn't use a MYSQL database as im using sessions.
Im trying to get the grand total of all items that are added to the cart however this is what im struggling with. I can get the total of each item added to the cart but not the total of all items together. Any help with this would be greatly appreciated.
Thankyou
PHP
<?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']);
}
?>
HTML FORM
<form action="" method="post" name="shoppingcart">
<?php
ob_start();
?>
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<th width="143" height="46" scope="col"> </th>
<th width="177" scope="col">Item Name</th>
<th width="155" scope="col">Unit Price</th>
<th width="130" scope="col">Quantity</th>
<th width="95" scope="col">Total</th>
</tr>
<?php
//Print all the items in the shopping cart
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
?>
<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 $item['total'] * $item['price']; ?></td>
</tr>
<?php
}
?>
</table>
<?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
<p align="left">
<label>
<p><a href="../products.php">Keep Shopping</a> - <a href="?empty">Empty Cart</a><br />
<br />
<input type="submit" name="update" id="update" value="Update Cart" />
<br />
<br />
</label>
</form>
Just replace this chunk of code, with my code below....should work...
<?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购物车的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!