我正在编写一个购物车并将我的数据存储在 $_SESSION 数组中,但想计算一个总数。在它下面是我认为可以做到这一点的代码,但它返回“1”而不是总数!
$total = array($_SESSION['qty'],$_SESSION['pr']);
/* 我会给你更多的代码...谢谢你的帮助!!这是我的 php 购物车的代码:
<?php
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();
if(isset($_POST['clear']) && ($_POST['clear'] == 'clear')) { session_destroy(); 未设置($_SESSION['数量']);未设置($_SESSION['item']);未设置($_SESSION['pr']);未设置($_POST['数量']);未设置($_POST['item']);未设置($_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 => $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)); }
}?>