PHP,关联数组与会议 [英] PHP, Associative arrays with sessions

查看:180
本文介绍了PHP,关联数组与会议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望在这里正确的方向推。我想创建使用会话相联系的数组。意图的是,当一个用户将一个项目到车,项目和数量被添加到阵列。我得到一个解析错误:语法错误,意想不到的T_DOUBLE_ARROW第13行错误我想我只是要有关它的错误

I'm just looking for a push in the right direction here. I am trying to create an associate array using a session. The intent is that when a user adds an item to the cart, the item and quantity is added to the array. I am getting a "Parse error: syntax error, unexpected T_DOUBLE_ARROW" error on line 13. I think I am just going about it wrong.

<?php
$action = $_GET['action'];
$itemID = $_GET['itemID'];
$qty = $_GET['quantity'];
$msg;
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart']['itemID'] = ('123' => 0, '456' => 0, '589' => 0, '101' => 0);
}
$cart = $_SESSION['cart'];
if ($action == 'add') {
/*  if (!in_array($cart, $itemID) {  // Add item to array
        $cart[$itemID]
        //array_push($cart, $itemID => $qty);
    }
    else { // Increase quantity */
        $_SESSION['cart'][$itemID] = $q;
        $v = $q + $qty;
        $_SESSION['cart'][$itemID] = $v;
//  }
    $msg = $qty.' of item # '.$itemID.' has been added to your cart.';
}
else { // remove from cart
    unset($_SESSION['cart'][$itemID]);
    $msg = $qty.' of item # '.$itemID.' has been removed to your cart.';
}
echo $msg; ?>

发生在第一,如果(!使用isset($ SESSION ['购物车'])语句创建时的错误。

Error occurs in first if(!isset($SESSION['cart']) statement upon creation.

我得到了一个错误,当我有array_push启用为好,但我不记得这是什么。

I was getting an error when I had the array_push enabled as well, but I do not recall what it was.

基本上功能是本 - 当用户(此时总数量的)添加项目到购物车正在创建的会话数组。当被添加的项目,该项目号码和数量被添加到阵列。添加另一个项目,它被添加。添加同一项目,数量增加一。 ..等等等等删除的项目。

Basically the function is this - SESSION array is created when user add item to cart (always quantity one at this time). When item is added, the item number and quantity is added to the array. Add another item, it is added. Add the same item, quantity increases by one. Remove an item .. so on and so forth.

希望你的想法。我有骨头书面和我在测试/故障排除阶段。正如我已经说过了,我想我创建阵列错误。我想创建数组空,并根据需要添加项目,但我已经尝试了几种不同的方式试图至少得到它的工作,上面显示的是最近的。同样,明明快到SESSION关联数组都错了。

Hopefully you get the idea. I've got the bones written and am in the test/troubleshoot phase. As I've said, I think I am creating the array wrong. I'd like to create the array empty and add items as needed, but I've tried a few different ways trying to at least get it working, the one shown above is the most recent. Again, obviously approaching the SESSION associative array all wrong.

任何意见将是极大的AP preciated!谢谢你在前进。

Any advice would be greatly appreciated! Thank you in advance.

推荐答案

您似乎已在第7行改变它是个错误。

Looks like you have an error at line 7 change it to

$_SESSION['cart']['itemID'] = array('123' => 0, '456' => 0, '589' => 0, '101' => 0);

您还可以检查如何定义一个阵列

you can also check how to define a array

这篇关于PHP,关联数组与会议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆