无论我在PHP中尝试什么,都无法向购物车添加多个项目
我感到很惭愧,一个多星期以来,我仍然不知道如何在购物车中插入一个新项目,如果用户已经点击了它,再次,如果用户之前点击了该项目,我必须增加数量 最后计算每个项目的数量、购物车中的项目总数、每个项目的数量和购物车中的总金额 “我的代码”仅使用上次单击的项目的详细信息修改购物车无论我在PHP中尝试什么,都无法向购物车添加多个项目,php,session,Php,Session,我感到很惭愧,一个多星期以来,我仍然不知道如何在购物车中插入一个新项目,如果用户已经点击了它,再次,如果用户之前点击了该项目,我必须增加数量 最后计算每个项目的数量、购物车中的项目总数、每个项目的数量和购物车中的总金额 “我的代码”仅使用上次单击的项目的详细信息修改购物车 session_start(); header("Access-Control-Allow-Origin:*"); header("Access-Control-Allow-Credentials:true"); header
session_start();
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Credentials:true");
header("Access-Control-Max-Age: 100000");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");
$output;
if(isset($_POST['item']) && isset($_POST['price'])) {
$_SESSION['purchase'] = ((isset($_SESSION['purchase'])) ? $_SESSION['purchase'] : 0);
$item_array = array(
$_POST['item'] => array(
'item' => $_POST['item'],
'price' => $_POST['price'],
'quantity' => 1
)
);
$purchasedItems = 0;
$_SESSION['purchasedItems'] = $purchasedItems;
if(!isset($_POST['action']) && !isset($_POST['action'])) {
if(isset($_SESSION['purchase'])) {
$available = 0;
foreach($_SESSION['purchase'] as $key => $value) {//ERROR: invalid argument supplid for foreach loop
if($value == $_POST['item']) {
$available++;
foreach($value as $a => $b) {
$_SESSION['purchase'][$key]['quantity'] = $b['quantity'] + 1;
}
}
else {
$_SESSION['purchase'][$item_array] = $item_array;
}
}
foreach($_SESSION['purchase'][$item_array] as $k => $v) {//ERROR: invalid argument supplied for foreach loop
if($_SESSION['purchase'][$v] == $_POST['item']) {
}
}
if($available < 1) {
$_SESSION['purchase'] = $item_array;
} else {
$_SESSION['purchase'] = $item_array;
}
}
}
$total = 0;
foreach($_SESSION['purchase'] as $key => $value) {//ERROR: invalid argument supplied for foreach loop
$total = $total + ($value['quantity'] * $value['price']);
print '<table><tr><th>Products </th><th> Quantity  </th><th>Price  </th><th> Total  </th><th> Actions </th></tr>';
print '<tr><td>' . "" . $value['item'] . '</td>' . '<td>' . $value['quantity'] . "" . '</td>' . '<td>' . number_format($value['quantity'] * $value['price'], 2) . '</td></tr>';
}
print '<tr><td> Total Price  </td><td>' . "" . $total . '</td></tr>';
print '<tr><td> Total Number Of Items  </td><td>' . "" . count($_SESSION['purchase']) . '</td></tr></table>';
var_dump($_SESSION['purchase']);
print $_SESSION['purchase']; // ERROR: array to string conversion
print session_status();
}
?>
session_start();
标题(“访问控制允许原点:”;
标题(“访问控制允许凭据:true”);
标题(“访问控制最大年龄:100000”);
标头(“访问控制允许标头:X-request-With、内容类型、来源、缓存控制、Pragma、授权、接受、接受编码”);
标题(“访问控制允许方法:PUT、POST、GET、OPTIONS、DELETE”);
美元产出;
如果(isset($_POST['item'])和&isset($_POST['price'])){
$_会话['purchase']=((isset($_会话['purchase'])?$_会话['purchase']:0);
$item\u数组=数组(
$\u POST['item']=>数组(
'item'=>$\u POST['item'],
'price'=>$\u POST['price'],
“数量”=>1
)
);
$purchasedItems=0;
$\会话['purchasedItems']=$purchasedItems;
如果(!isset($_POST['action'])和&!isset($_POST['action']){
如果(isset($_会话['purchase'])){
$available=0;
foreach($\会话['purchase']作为$key=>$value){//错误:foreach循环的参数supplid无效
如果($value=$\u POST['item']){
$available++;
foreach($a=>b的值){
$\会话['purchase'][$key]['quantity']=$b['quantity']+1;
}
}
否则{
$\会话['purchase'][$item\u数组]=$item\u数组;
}
}
foreach($\u会话['purchase'][$item\u array]作为$k=>$v){//错误:为foreach循环提供的参数无效
如果($_SESSION['purchase'][$v]=$_POST['item']){
}
}
如果($available<1){
$\u会话['purchase']=$item\u数组;
}否则{
$\u会话['purchase']=$item\u数组;
}
}
}
$total=0;
foreach($\u会话['purchase']作为$key=>$value){//错误:为foreach循环提供的参数无效
$total=$total+($value['quantity']*$value['price']);
打印“产品数量和价格行动总数”;
打印'.'.'.$value['item'].'.$value['quantity'.'.'.'.''数字'.''格式($value['quantity']*$value['price'],2)。'';
}
打印“总价”。“$Total.”;
打印“项目总数”。计数($会话['purchase']);
var_dump($_会话['purchase']);
打印$_会话['purchase'];//错误:数组到字符串的转换
打印会话_状态();
}
?>
在这里,我将抛出的错误包含在相应的代码行中。一些反馈:
- 你不应该从邮局得到(信任)价格李>
- 而且您可能不应该在POST中构建自定义数组李>
- 对我来说,“购物车”而不是“购买”读代码更有意义
- 只需构建一个数组,其中项目编号作为键,数量作为值。如果itemno/key已存在,则添加值
session_start();
if(!isset($_REQUEST['item'])) die();
//sanitize indata. here we'll only accept ints.
$item = floor($_REQUEST['item']);
//assume we're sending quantity as well
$qty = 1;
if (isset($_REQUEST['qty'])) $qty = floor($_REQUEST['qty']);
//maybe some more sanity here, negative values etc..
//
//check if we need to initialize cart
if (!is_array($_SESSION['cart'])) $_SESSION['cart'] = array();
//
//either add item as new key or increase value of quantity
if (in_array($item, array_keys($_SESSION['cart'])) {
//add qty
$_SESSION['cart'][$item]['qty'] += $qty;
} else {
//init
$_SESSION['cart'][$item] = array();
//setup a pointer for easier code
$a = &$_SESSION['cart'][$item];
$a['qty'] = $qty;
//look up price and name for the item in backend
$rec = [select Price, Name from bla bla where ItemNo=$item]
$a['price'] = $rec['Price'];
$a['name'] = $rec['Name'];
//actually, you would probably like to verify that item actually exists in database before even init the array so swap above..
}
//calc totals
$total = 0;
foreach($_SESSION['cart'] $item=>$itemRec) {
$total += $itemRec['qty'] * $itemRec['price'];
}
仅供参考,您正在仔细检查post操作:!isset($_POST['action'])&&!isset($_POST['action'])请您编辑代码并更正,然后发送链接或电子邮件给我,因为我对这个大的感谢@Tenson感到困惑。谢谢你的帮助。我会把它改编成适合我的剧本。请考虑把我的帖子标记为已接受的答案。祝你的php事业好运!请问@Tenson我怎样才能在这里发布被接受的答案?我真的不知道怎么做。谢谢。@smacaz,我的答案左上角应该有一个灰色的格子标记。您可以单击它,它将变为绿色。