我正在编写购物车,并将我的数据存储在$ _SESSION数组中,但想计算总数。在它下面我认为可以做到这一点的代码,但它返回'1'而不是总计!
$total = array($_SESSION['qty'],$_SESSION['pr']);
/ *我会给你更多代码......谢谢你的帮助!! 这是我的php购物车的代码:
<?php
function item_list() { 如果(isset($ _ SESSION [ '数量'])){ $ 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();
if(isset($ _ POST ['clear'])&amp;&amp;($ _POST ['clear'] =='clear')) { session_destroy(); 未设置($ _ SESSION [ '数量']); 未设置($ _ SESSION [ '项目']); 未设置($ _ SESSION [ 'PR']); 未设置($ _ POST [ '数量']); 未设置($ _ POST [ '项']); 未设置($ _ POST [ 'PR']); }
if(!isset($ _ SESSION ['qty']))$ _SESSION ['qty'] = array(); if(!isset($ _ SESSION ['item']))$ _SESSION ['item'] = array(); if(!isset($ _ SESSION ['pr']))$ _SESSION ['pr'] = array();
如果(isset($ _ POST [ '数量'])) {
foreach($_POST['qty'] as $value)
{
if(!$value == '') array_push($_SESSION['qty'], filter_var($value,
FILTER_SANITIZE_SPECIAL_CHARS)); } foreach($ _ POST ['item'] as $ key =&gt; $ value) { if(!$ _ POST ['qty'] [$ key] =='')array_push($ _ SESSION ['item'],filter_var($ value, FILTER_SANITIZE_SPECIAL_CHARS)); } foreach($ _ POST ['pr'] as $ key =&gt; $ value) { if(!$ _ POST ['qty'] [$ key] =='')array_push($ _ SESSION ['pr'],filter_var($ value, FILTER_SANITIZE_SPECIAL_CHARS)); }
}&GT;
答案 0 :(得分:1)
这是构建购物车的一种奇怪的方式,但是这里是如何使用该结构:
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
}
如果您想获得购物车的所有数量和成本:
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
我建议使用更好的结构,例如:
$_SESSION['cart']['items'] = array(
array(
'name' => 'Screwdriver',
'price' => 5,
'qty' => 2,
),
array(
'name' => 'Hammer',
'price' => 10,
'qty' => 1,
)
);
答案 1 :(得分:0)
根据您的购物车数组,它无法容纳多个产品,您必须使用这样的多维数组
$_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),
)
然后你可以像这样编写你的代码
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
}
}
答案 2 :(得分:0)
你应该自己创建一个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();
这将允许您随着时间的推移更轻松地更改代码。