【问题标题】:How to insert a new array key value and store in a session using PHP?如何使用 PHP 插入新的数组键值并存储在会话中?
【发布时间】:2014-09-15 13:29:23
【问题描述】:

我需要一点帮助。在我的页面中,我有一个摘要产品列表,并且在该页面中,我需要将我所有的父 ID 存储在一个会话中。但只有我可以存储的最后一个 id。如何在会话中将所有产品 ID 存储为键,将数量存储为值?

这是我的示例代码:

fprintr($get_product);

        foreach($get_product as $product_detail) {

            if ($product_detail['image']) {
                $image = $this->model_tool_image->resize($product_detail['image'], $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
            } else {
                $image = false;
            }

            $search_data[] = array(
                'product_id' => $product_detail['product_id'],
                'code' => $product_detail['code'],
                /*'name'        => str_replace('||','<br/>',$result['name']),*/
                'product_name' => str_replace('||',' ', $product_detail['product_name']),
                'description' => html_entity_decode($product_detail['description'], ENT_QUOTES, 'UTF-8'),
                'image' => $image,
                'price' => $this->currency->format($this->tax->calculate($product_detail['price'], 0, 0)),
                'quantity' => $product_detail['quantity'],
                'update' => $this->url->link('giftregistry/registrymanage/viewRegistryDetail'),
                'remove' => $this->url->link('giftregistry/registrymanage/viewregistrydetail', '&remove=' . $product_detail['product_id'] . '&code=' . $product_detail['code']),
            );

            $this->session->data['cart']= array(
                $product_detail['product_id'].'::' => $product_detail['quantity']
            );

        }

        fprintr($this->session->data);

这是我的数组示例:

这是我的产品从数据库中的初始加载

Array
(
    [0] => Array
        (
            [product_id] => 56
            [product_name] => Luminarc Rhodes Ice Bucket
            [price] => 300.00
            [quantity] => 1
            [code] => S6-214347
            [description] => <p>Luminarc Rhodes Ice Bucket</p>

            [image] => data/metro/products/Wine and Dine/08.jpg
        )

    [1] => Array
        (
            [product_id] => 57
            [product_name] => Regent Cheese Board with Knife
            [price] => 2949.00
            [quantity] => 1
            [code] => S6-214347
            [description] => <p>Regent Cheese Board with Knife</p>

            [image] => data/metro/products/Wine and Dine/02.jpg
        )

)

这是我更新后的会话购物车

[cart] => Array
        (
            [57::] => 1
        )

如您所见,存储了最后一个产品 ID。如何保存所有产品 ID?

【问题讨论】:

    标签: php session


    【解决方案1】:

    使用这个

    $this->session->data['cart'][] = array(
            $product_detail['product_id'].'::' => $product_detail['quantity']
    );
    

    这将解决您的问题。

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多