[英]how can i write an algorithm to multiply the total * quantity in a shopping cart using php $_SESSION?
I am writing a shopping cart and have my data stored in the $_SESSION array, but would like to calculate a total.我正在编写一个购物车并将我的数据存储在 $_SESSION 数组中,但想计算总数。 below it the code I thought would work to do this, but it returns '1' in stead of a total!
在它下面是我认为可以做到这一点的代码,但它返回“1”而不是总数!
$total = array($_SESSION['qty'],$_SESSION['pr']);
/* I'll give you more code...thanks for your help!! /* 我会给你更多的代码...谢谢你的帮助!! here is the code for my php cart:
这是我的 php 购物车的代码:
<?php
function item_list() { if(isset($_SESSION['qty'])){ $total = array($_SESSION['qty'],$_SESSION['pr']);
function item_list() { if(isset($_SESSION['qty'])){ $total = array($_SESSION['qty'],$_SESSION['pr']);
foreach($_SESSION['qty'] as $key => $value)
{?>
<tr>
<td align="center"><?php echo $_SESSION['item'][$key]; ?></td>
<td align="center"><?php echo $value; ?></td>
<td align="center"><?php echo $_SESSION['pr'][$key]; ?></td>
<td align="center"><?php echo array_product($total); ?>
</tr><?php
}
}
}
}
session_start();
session_start();
if(isset($_POST['clear']) && ($_POST['clear'] == 'clear')) { session_destroy();
if(isset($_POST['clear']) && ($_POST['clear'] == 'clear')) { session_destroy(); unset($_SESSION['qty']);
未设置($_SESSION['qty']); unset($_SESSION['item']);
未设置($_SESSION['item']); unset($_SESSION['pr']);
未设置($_SESSION['pr']); unset($_POST['qty']);
未设置($_POST['数量']); unset($_POST['item']);
未设置($_POST['item']); unset($_POST['pr']);
未设置($_POST['pr']); }
}
if(!isset($_SESSION['qty'])) $_SESSION['qty'] = array();
if(!isset($_SESSION['qty'])) $_SESSION['qty'] = array(); if(!isset($_SESSION['item'])) $_SESSION['item'] = array();
if(!isset($_SESSION['item'])) $_SESSION['item'] = array(); if(!isset($_SESSION['pr'])) $_SESSION['pr'] = array();
if(!isset($_SESSION['pr'])) $_SESSION['pr'] = array();
if(isset($_POST['qty'])) {
if(isset($_POST['qty'])) {
foreach($_POST['qty'] as $value)
{
if(!$value == '') array_push($_SESSION['qty'], filter_var($value,
FILTER_SANITIZE_SPECIAL_CHARS));
FILTER_SANITIZE_SPECIAL_CHARS)); } foreach($_POST['item'] as $key => $value) { if(!$_POST['qty'][$key] == '') array_push($_SESSION['item'], filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS));
} foreach($_POST['item'] as $key => $value) { if(!$_POST['qty'][$key] == '') array_push($_SESSION['item'], filter_var( $value, FILTER_SANITIZE_SPECIAL_CHARS)); } foreach($_POST['pr'] as $key => $value) { if(!$_POST['qty'][$key] == '') array_push($_SESSION['pr'], filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS));
} foreach($_POST['pr'] as $key => $value) { if(!$_POST['qty'][$key] == '') array_push($_SESSION['pr'], filter_var( $value, FILTER_SANITIZE_SPECIAL_CHARS)); }
}
}}
?>
?>
That is a strange way to structure a shopping cart, but here's how to do it with that structure:这是构建购物车的一种奇怪的方式,但这里是如何使用该结构来实现的:
foreach($_SESSION['qty'] as $key => $value)
{
$total = $_SESSION['qty'][$key] * $_SESSION['pr'][$key];
?>
<tr>
<td align="center"><?php echo $_SESSION['item'][$key]; ?></td>
<td align="center"><?php echo $value; ?></td>
<td align="center"><?php echo $_SESSION['pr'][$key]; ?></td>
<td align="center"><?php echo $total; ?>
</tr><?php
}
If you wanted to get a total of all quantity and cost of the cart:如果您想获得购物车的所有数量和成本的总和:
function getTotals()
{
$total = array('qty' => 0, 'price' => 0);
foreach($_SESSION['qty'] as $key => $qty)
{
$total['qty'] += $qty;
$total['price'] += ($_SESSION['pr'][$key] * $qty)
}
return $total;
}
$total = getTotals();
echo $total['qty']; // output the total quantity of items
echo $total['price']; // output the total cost for all items and quantity
I would recommend a better structure though, something like:不过,我会推荐一个更好的结构,例如:
$_SESSION['cart']['items'] = array(
array(
'name' => 'Screwdriver',
'price' => 5,
'qty' => 2,
),
array(
'name' => 'Hammer',
'price' => 10,
'qty' => 1,
)
);
As per your cart array it is not able to hold multiple products you have to use multy dimensional array like this根据您的购物车数组,它无法容纳多个产品,您必须使用这样的多维数组
$_SESSION['cart_items'] = array(
array( "qty"=>5, "item"=>"tshirt", "pr"=>50.20),
array( "qty"=>2, "item"=>"Cell Phone", "pr"=>50.20),
array( "qty"=>7, "item"=>"", "pr"=>50.20),
)
then you can write your code like this然后你可以像这样写你的代码
function item_list()
{
foreach($_SESSION['cart_items'] as $item_array)
{?>
<tr>
<td align="center">Item:<?php echo $item_array['item']; ?></td>
<td align="center">Qty: <?php echo $item_array['qty']; ?></td>
<td align="center">Price :<?php echo $item_array['pr']; ?></td>
<td align="center">Total : <?php echo $item_array['qty'] * $item_array['pr']; ?>
</tr><?php
}
}
You should create yourself a Card
class that is able to import/export data from the $_SESSION
superglobal (or some other array if you mock it for tests, testing with $_SESSION
can be akward) which is able to handle your data-structure easily and can calculate the total, too:您应该为自己创建一个
Card
类,它能够从$_SESSION
超全局变量(或其他一些数组,如果您模拟它进行测试,使用$_SESSION
进行测试可能会很尴尬)导入/导出数据,它能够轻松处理您的数据结构也可以计算总数:
$cart = new Cart();
$cart->importFromArray($_SESSION);
// or:
$cart->importFromArray($_SESSION['cart']);
// later on:
$total = $cart->getTotal();
// somewhere else:
$cart->addItem(...);
...
$_SESSION['cart'] = $cart->exportToArray();
That will allow you to more easily change the code over time.随着时间的推移,这将允许您更轻松地更改代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.