doutao1282 2016-03-30 23:16
浏览 38

PHP Session多维数组未被赋值任何值

After a button is being pressed, 3 values are put in into a multidimensional array in PHP. I've checked the values before assigning in the same brackets and it seems to have the correct value. However, when I add the values like this:

if (isset($_POST['add_to_cart'])) {
    $count = count($_SESSION['shopping_cart']);
    echo "Count: $count<br />";

    $_SESSION['shopping_cart'][$count]['product_id'] = $_POST['product_id'];
    $_SESSION['shopping_cart'][$count]['tier'] = $_POST['tier'];
    $_SESSION['shopping_cart'][$count]['division'] = $_POST['division'];
}

The output shows that the array $_SESSION['shopping_cart'] is empty and has no values.

if (empty($_SESSION['shopping_cart'])) {
    echo "Your cart is empty.<br />";
}
else {
    //Display products in cart
    foreach($_SESSION['shopping_cart'] as $id => $product) {
        echo $product['tier'] . $product['division'] . "<br />";
    }
}

I've came to the conclusion that I assign the values in a wrong way. What am I doing wrong? Thanks for the help!

EDIT: forgot to add that the array is already initialized at the beginning!

if(!isset($_SESSION['shopping_cart'])) {
    $_SESSION['shopping_cart'] = array();
}
  • 写回答

1条回答 默认 最新

  • duanbushi1867 2016-03-30 23:29
    关注

    I forgot to add the session_start(); to the code. That's why the Session array didn't work! Thanks for the help @Will

    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析